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.
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:
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
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 uses instance_variable_get()
and instance_variable_set()
. Even worse, it aliases instance_variable_get()
to just be g()
.
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)
.
This appears to be a binary tree implementation in very few lines. I apologize if my understanding of the ruby syntax is limited:
class Tree // defining the class Tree
def initialize *d; // defines the initializer
@d = d; // sets the node value
end
def to_s; // defines the to_s(tring) function
@l || @r ? ",>" : @d; // conditional operator. Can't tell exactly what this
// function is intending. Would think it should make a
// recursive call or two if it's trying to do to_string
end
def total; // defines the total (summation of all nodes) function
@d.is_a ? (Numeric) // conditional operator. Returns
? @d // @d if the data is numeric
: 0 // or zero
+ (@l ? @l.total : 0) // plus the total for the left branch
+ (@r ? @r.total : 0) // plus the total for the right branch
end
def insert d // defines an insert function
?? // but I'm not going to try to parse it...yuck
end
Hope that helps some... :/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With