如果这段代码不是玩笑,那么它到底是如何工作的呢?
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]: 0)+(@[email protected]: 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
有人愿意尝试解释一下这是做什么的吗? 它出现在我询问的关于代码 的问题中的答案太聪明了。 但我太聪明了,无法判断这是否只是一个笑话。 如果不是,我很想知道它是如何工作的,是否有人愿意解释。
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]: 0)+(@[email protected]: 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:发布原始混淆示例的人给出了 他的答案中的实际源代码。 他还发布了
这是一些很好的混淆代码。 与大多数混淆的代码一样,它主要是大量的三元运算符,并且顽固地拒绝在正常人会添加的地方添加空格。 这里基本上是更通常编写的相同内容:
insert 方法在语法上无效(它在某一部分缺少方法名称),但据我所知,这本质上就是它的作用。 该方法中的混淆非常严重:
它使用
instance_variable_get()
和instance_variable_set()
,而不是仅仅执行@l = another
>。 更糟糕的是,它将instance_variable_get()
别名为g()
。它
它将大部分功能包装在 lambda 函数中,并将
@l
的名称传递给该函数。 然后,它使用鲜为人知的语法func[arg1, arg2]
调用此函数,该语法相当于func.call(arg1, arg2)
。EDIT: 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:
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:
Instead of just doing
@l = whatever
, it usesinstance_variable_get()
andinstance_variable_set()
. Even worse, it aliasesinstance_variable_get()
to just beg()
.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 offunc[arg1, arg2]
, which is equivalent tofunc.call(arg1, arg2)
.这似乎是一个二叉树的实现,只需很少的几行。 如果我对 ruby 语法的理解有限,我深表歉意:
希望对一些人有所帮助......:/
This appears to be a binary tree implementation in very few lines. I apologize if my understanding of the ruby syntax is limited:
Hope that helps some... :/
事情是这样开始的:
我想我在缩短它的时候实际上把它弄坏了。 九行版本不太有效。 无论如何,我玩得很开心。 :P
这是我最喜欢的部分:
这实际上是利用并行分配来保存几个字符。 您可以将此行扩展为:
It started off as this:
I think I actually broke it when I was shortening it down. The nine-line version doesn't quite work. I had fun regardless. :P
This was my favourite part:
This is acutally making use of parallel assignment to save a couple characters. You could expand this line to:
我发布了原始代码。 抱歉,但我没有费心去检查我是否做得对,并且由于小于符号,一堆东西被删除了。
这就是它应该的样子。
I posted the original code. Sorry, but I didn't bother to check that I even did it right, and a bunch of stuff got stripped out because of less than signs.
That's what it should look like.