如果这段代码不是玩笑,它究竟是如何工作的? [英] If this code is not a joke, how on earth does it work?

查看:40
本文介绍了如果这段代码不是玩笑,它究竟是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Tree
  def initialize*d;@d,=d;end
  def to_s;@l||@r?",>":@d;end
  def total;(@d.is_a?(Numeric)?@d:0)+(@l?@l.total: 0)+(@r?@r.total: 0);end
  def insert d
    alias g instance_variable_get
    p=lambda{|s,o|d.to_s.send(o,@d.to_s)&&
      (g(s).nil??instance_variable_set(s,Tree.new(d)):g(s).insert(d))}
    @d?p[:@l,:]:@d=d
  end
end

有人愿意尝试解释一下这是做什么的吗?它出现在我问的有关代码的问题中的答案 太聪明了.但我太聪明了,无法判断这是否只是一个玩笑.如果不是,我很想知道它是如何工作的,如果有人愿意解释的话.

Would anyone like to take a stab at explaining what this does? It appeared as an answer in a question I asked about code that is too clever. But it's too clever for me to tell whether it's simply a joke. If it's not, I'd be interested to know how it works, should anyone care to explain.

推荐答案

发布原始混淆示例的人给出了 他的回答中的实际源代码.他还发布了一个 更正了混淆代码的版本,因为正如我所指出的,即使您删除了时髦的语法,其中一些也没有任何意义.

The person who posted the original obfuscated example gave the actual source code in his answer. He also posted a corrected version of the obfuscated code, because as I noted, some of it didn't make sense even when you removed the funky syntax.

这是一些很好的混淆代码.与大多数混淆的代码一样,它主要是大量的三元运算符和顽固的拒绝在正常人会的地方放置空格.这里基本上是更正常的写法:

That is some nicely obfuscated code. As with most obfuscated code, it's mostly a lot of ternary operators and a stubborn refusal to put in whitespace where a normal person would. Here is basically the same thing written more normally:

class Tree
  def initialize(*d)
    @d,  = d # the comma is for multiple return values,
             # but since there's nothing after it,
             # all but the first are discarded.
  end
  def to_s
    @l || @r ? ",>" : @d
  end
  def total
    total = @d.is_a?(Numeric) ? @d : 0
    total += @l.total if @l
    total += @r.total if @r
  end
  def insert(arg)
    if @d
      if @l
        @l.insert(arg)
      else
        @l = Tree.new(arg)
      end
    else
      @d = arg
    end
  end
end

insert 方法在语法上无效(它在某一部分缺少方法名称),但据我所知,它基本上就是这样做的.该方法中的混淆非常严重:

The insert method is not syntactically valid (it's missing a method name at one part), but that's essentially what it does as far as I can tell. The obfuscation in that method is pretty thick:

  1. 它使用instance_variable_get()instance_variable_set() 而不是仅仅执行@l = 任何.更糟糕的是,它将 instance_variable_get() 别名为 g().

  1. Instead of just doing @l = whatever, it uses instance_variable_get() and instance_variable_set(). Even worse, it aliases instance_variable_get() to just be g().

它将大部分功能封装在一个 lambda 函数中,并将 @l 的名称传递给该函数.然后它使用鲜为人知的 func[arg1, arg2] 语法调用这个函数,它等价于 func.call(arg1, arg2).

It wraps most of the functionality in a lambda function, to which it passes the name of the @l. Then it calls this function with the lesser-known syntax of func[arg1, arg2], which is equivalent to func.call(arg1, arg2).

这篇关于如果这段代码不是玩笑,它究竟是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆