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

时间:2009-04-03 22:24:47

标签: ruby obfuscation

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

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

4 个答案:

答案 0 :(得分:15)

编辑:发布原始混淆示例的人在答案中提供了the actual source code。他还发布了一个corrected version of the obfuscated code,因为正如我所说,即使你删除了时髦的语法,其中一些也没有意义。

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

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. 它将大部分功能包含在lambda函数中,并传递@l的名称。然后它使用鲜为人知的func[arg1, arg2]语法调用此函数,该函数相当于func.call(arg1, arg2)

答案 1 :(得分:9)

这似乎只是很少的行中的二叉树实现。如果我对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

希望有所帮助......:/

答案 2 :(得分:8)

它开始于此:

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

我认为当我缩短它时,我确实打破了它。九行版本不太适用。无论如何我都很开心。 :P

这是我最喜欢的部分:

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

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

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

答案 3 :(得分:7)

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

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

这应该是什么样子。