6
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

誰かがこれが何をするのかを説明するのに挑戦したいですか?それは私があまりにも賢いコードについて尋ねた質問の答えとして現れました。しかし、それが単なる冗談であるかどうかを判断するのは賢すぎます。そうでない場合は、誰かが説明したいと思ったら、それがどのように機能するかを知りたいと思います。

4

4 に答える 4

15

編集:元の難読化された例を投稿した人は、回答で実際のソースコードを提供しました。彼は難読化されたコードの修正版も投稿しました。私が指摘したように、ファンキーな構文を削除しても意味をなさないものもあったからです。

これはうまく難読化されたコードです。ほとんどの難読化されたコードと同様に、ほとんどの場合、多くの三項演算子があり、通常の人が空白を挿入することを頑固に拒否しています。これは基本的に同じことをより普通に書いたものです:

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

挿入メソッドは構文的に有効ではありません (メソッド名の一部が欠落しています) が、私が知る限り、基本的にはそれが機能します。そのメソッドの難読化はかなり厚いです:

  1. を行うだけでなく@l = whatever、 と を使用instance_variable_get()instance_variable_set()ます。instance_variable_get()さらに悪いことに、単に beにエイリアスされますg()

  2. ほとんどの機能をラムダ関数にラップし、そこに の名前を渡し@lます。次に、あまり知られていない の構文でこの関数を呼び出します。func[arg1, arg2]これは と同等func.call(arg1, arg2)です。

于 2009-04-03T23:04:11.870 に答える
9

これは、ごくわずかな行での二分木の実装のようです。ルビー構文の理解が限られている場合は、お詫び申し上げます。

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

それがいくつかの助けになることを願っています...:/

于 2009-04-03T22:40:42.597 に答える
7

それは次のように始まりました:

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

短くしているときに実際に壊したと思います。9 行のバージョンはまったく機能しません。あいかわらず楽しかった。:P

これは私のお気に入りの部分でした:

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

これは実際に並列代入を利用して数文字を節約しています。この行を次のように展開できます。

def initialize(*d)
  @d = d[0]
end
于 2009-04-03T23:45:12.413 に答える
6

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.

于 2009-04-04T00:05:10.337 に答える