问题
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.
回答1:
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 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)
.
回答2:
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... :/
回答3:
It started off as this:
class Tree
include Comparable
attr_reader :data
# Create a new node with one initial data element
def initialize(data=nil)
@data = data
end
# Spaceship operator. Comparable uses this to generate
# <, <=, ==, =>, >, and between?
def <=>(other)
@data.to_s <=> other.data.to_s
end
# Insert an object into the subtree including and under this Node.
# First choose whether to insert into the left or right subtree,
# then either create a new node or insert into the existing node at
# the head of that subtree.
def insert(data)
if !@data
@data = data
else
node = (data.to_s < @data.to_s) ? :@left : :@right
create_or_insert_node(node, data)
end
end
# Sum all the numerical values in this tree. If this data object is a
# descendant of Numeric, add @data to the sum, then descend into both subtrees.
def total
sum = 0
sum += @data if (@data.is_a? Numeric)
sum += [@left, @right].map{|e| e.total rescue 0}.inject(0){|a,v|a+v}
sum
end
# Convert this subtree to a String.
# Format is: <tt>\<data,left_subtree,right_subtree></tt>.
# Non-existant Nodes are printed as <tt>\<></tt>.
def to_s
subtree = lambda do |tree|
tree.to_s.empty? ? "<>" : tree
end
"<#{@data},#{subtree[@left]},#{subtree[@right]}>"
end
private ############################################################
# Given a variable-as-symbol, insert data into the subtree incl. and under this node.
def create_or_insert_node(nodename, data)
if instance_variable_get(nodename).nil?
instance_variable_set(nodename, Tree.new(data))
else
instance_variable_get(nodename).insert(data)
end
end
end
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:
def initialize*d;@d,=d;end
This is acutally making use of parallel assignment to save a couple characters. You could expand this line to:
def initialize(*d)
@d = d[0]
end
回答4:
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.
class Tree
def initialize*d;@d,=d;end
def to_s;@l||@r?"<#{@d},<#{@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,:<]||p[:@r,:>]:@d=d
end
end
That's what it should look like.
来源:https://stackoverflow.com/questions/715951/if-this-code-is-not-a-joke-how-on-earth-does-it-work