2009-04-03 6 views
7
class Tree 
    def initialize*d;@d,=d;end 
    def to_s;@l||@r?",>":@d;end 
    def total;(@d.is_a?(Numeric)[email protected]: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 

これが何をしているか説明するのに誰かが欲しいと思いますか? too cleverというコードについて私が尋ねた質問に答えとして出てきました。しかし、それが単なる冗談であるかどうかを私が知るにはあまりにも賢いです。そうでなければ、誰かが説明しなければならない場合、それがどのように機能するか知りたいと思うだろう。このコードが冗談でない場合、どのように動作しますか?

+0

http://www.rubyinside.com/advent2006/4-ruby-obfuscation.htmlと比較して何もありません。P – Aziz

+0

@firoso:ITフォーラムをトロールしたい場合は、slashdotに戻ります。 – Juliet

答えて

15

EDIT:オリジナルの難読化の例を掲示人は彼の答えに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 

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

  1. だけではなく@l = whateverをやって、それがinstance_variable_get()instance_variable_set()を使用しています。さらに悪いことに、別名instance_variable_get()はちょうどg()になります。

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

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 

...:私は、Rubyの構文の私の理解が限られている場合は謝罪/

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 [email protected] 
     @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 

これはacutallyカップルの文字を保存するために、並列割り当てを利用しています。この行を次のように展開することができます。

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

最初の投稿を読んだときに誓ったのは、なぜ以前にそれを見たのだろうと思っていたのです。それから私はクラスを書くために恐ろしいことを吹き飛ばしている私たちの束を思い出しました。 –

+0

ハハ、そうです。ああ、2150. – Burke

+0

私はちょうど 'def initialize d; @ d = d; end'を実行するよりも、私の並列割り当てのトリックが実際に長い文字であることを認識しました。それはかなりもう少し素晴らしいです、私は思う:P – Burke

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)[email protected]: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,:<]||p[:@r,:>]:@d=d 
    end 
end 

これは、そのようになります。

関連する問題