class Tree
def initialize*d;@d,=d;end
def to_s;@l||@r?",>":@d;end
def total;(@d.is_a?(Numeric)?@d:0)+(@[email protected] /cdn-cgi/l/email-protection: 0)+(@[email protected] /cdn-cgi/l/email-protection: 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
有人愿意尝试解释一下这是做什么的吗?它出现在我询问的有关代码的问题中的答案是太聪明了 https://stackoverflow.com/questions/708428/whats-an-example-of-ruby-code-thats-too-clever。但我太聪明了,无法判断这是否只是一个笑话。如果不是,我很想知道它是如何工作的,是否有人愿意解释。
编辑:发布原始混淆示例的人给出了实际的源代码 https://stackoverflow.com/questions/715951/if-this-code-is-not-a-joke-how-on-earth-does-it-work/716110#716110在他的回答中。他还发布了一个混淆代码的更正版本 https://stackoverflow.com/questions/715951/if-this-code-is-not-a-joke-how-on-earth-does-it-work/716139#716139,因为正如我所指出的,即使您删除了时髦的语法,其中一些内容也没有意义。
这是一些很好混淆的代码。与大多数混淆的代码一样,它主要是大量的三元运算符,并且顽固地拒绝在正常人会添加的地方添加空格。这基本上是更正常写的相同内容:
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 方法在语法上并不有效(它在某一部分缺少方法名称),但据我所知,这本质上就是它的作用。该方法中的混淆非常严重:
而不是仅仅做@l = whatever
, 它用instance_variable_get()
and instance_variable_set()
。更糟糕的是,它还别名instance_variable_get()
就只是g()
.
它将大部分功能包装在 lambda 函数中,并将 lambda 函数的名称传递给该函数。@l
。然后它使用鲜为人知的语法调用该函数func[arg1, arg2]
,这相当于func.call(arg1, arg2)
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)