如果这段代码不是一个笑话,它究竟是如何工作的?

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 

有人愿意尝试解释这是做什么的吗? 它似乎是一个问题的答案,我问的代码太聪明了 。 但是,对我来说,判断这只是一个笑话真是太聪明了。 如果不是,我有兴趣知道它是如何工作的,如果有人愿意解释的话。

编辑:发布原始混淆示例的人在他的答案中给出了实际的源代码 。 他还发布了混淆代码的更正版本 ,因为正如我所说,即使你删除了时髦的语法,其中一些也没有意义。

这是一些很好的混淆代码。 与大多数混淆代码一样,它主要是许多三元运算符,并且顽固地拒绝将正常人放入空格。 这里写的基本上是相同的东西:

 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方法在语法上没有效果(它在一个部分缺少一个方法名称),但就我所知,这基本上就是它的作用。 该方法的混淆非常厚:

  1. 它不使用@l = whatever ,而是使用instance_variable_get()instance_variable_set() 。 更糟糕的是,它将instance_variable_get()别名为g()

  2. 它将大部分function包含在lambda函数中,并将其传递给@l的名称。 然后它用func[arg1, arg2]的鲜为人知的语法调用这个函数,它相当于func.call(arg1, arg2)

这似乎只是几行中的二叉树实现。 如果我对ruby语法的理解有限,我道歉:

 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 

希望有所帮助……:/

它开始于此:

 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: \. # Non-existant Nodes are printed as \<>. 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 

我觉得当我把它缩短时我真的打破了它。 九行版本不太适用。 无论如何我都很开心。 :P

这是我最喜欢的部分:

 def initialize*d;@d,=d;end 

这实际上是利用并行分配来保存几个字符。 您可以将此行扩展为:

 def initialize(*d) @d = d[0] end 

我发布了原始代码。 对不起,但我没有费心去检查我是否做得对,而且由于标志不足,一堆东西被剥夺了。

 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 

这应该是什么样子。