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.
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).