title: How to implement an in-place map method (map!) in ruby on a binary-tree?
body: <p>I'm trying to implement an in-place method on an object and I'm getting the following error:</p>
  
  <pre><code>Can't change the value of self (SyntaxError)
  </code></pre>
  
  <p>When trying to do</p>
  
  <pre><code>self = map(&amp;block)
  </code></pre>
  
  <p>on the following object</p>
  
  <pre><code>class Node
    include Enumerable
  
    # binary tree representation
    attr_accessor :value, :left, :right
    def initialize(value=nil, left=nil, right=nil)
      @value, @left, @right = value, left, right
    end
  
    def map(&amp;block)
      res = Array.new
      res &lt;&lt; yield(value) if value
      res &lt;&lt; left.map(&amp;block) if left 
      res &lt;&lt; right.map(&amp;block) if right
      res.flatten
    end
  
    def map!(&amp;block)
      self = self.map(&amp;block)
    end
  
    def to_a
      map { |a| a }
    end
  end
  </code></pre>
  
  <p>I've also tried to use some of Enumerable's destructive methods to no avail</p>
  
  <pre><code>map(&amp;block).collect!
  </code></pre>
  
  <p>What is wrong with my approach and how would you implement a function like this?</p>
  
  <p><strong>Update</strong></p>
  
  <p>To clarify the idea is to implement map on a binary tree, which the map method above does successfully, my problem is translating that method into an in-place version.</p>
  
  <pre><code>irb(main):001:0&gt; require './node.rb'
  =&gt; true
  irb(main):002:0&gt; root = @root = Node.new(1, Node.new(2, Node.new(3), Node.new(4)),Node.new(5, Node.new(6), Node.new(7)))
  =&gt; #&lt;Node:0x78803f58 @value=1, @left=#&lt;Node:0x78abc090 @value=2, @left=#&lt;Node:0x78abc0f0 @value=3, @left=nil, @right=nil&gt;, @right=#&lt;Node:0x78abc0c0 @value=4, @left=nil, @right=nil&gt;&gt;, @right=#&lt;Node:0x78803f88 @value=5, @left=#&lt;Node:0x78abc060 @value=6, @left=nil, @right=nil&gt;, @right=#&lt;Node:0x78abc030 @value=7, @left=nil, @right=nil&gt;&gt;&gt;
  irb(main):003:0&gt; root.map { |a| a * 3 }
  =&gt; [3, 6, 9, 12, 15, 18, 21]
  irb(main):004:0&gt; root
  =&gt; #&lt;Node:0x778fb828 @value=1, @left=#&lt;Node:0x778fb9c0 @value=2, @left=#&lt;Node:0x778fba68 @value=3, @left=nil, @right=nil&gt;, @right=#&lt;Node:0x778fb9f0 @value=4, @left=nil, @right=nil&gt;&gt;, @right=#&lt;Node:0x778fb888 @value=5, @left=#&lt;Node:0x778fb930 @value=6, @left=nil, @right=nil&gt;, @right=#&lt;Node:0x778fb8b8 @value=7, @left=nil, @right=nil&gt;&gt;&gt;
  irb(main):005:0&gt; root.map! { |a| a * 3 }
  =&gt; [3, 6, 9, 12, 15, 18, 21]]
  irb(main):006:0&gt; root
  =&gt; [3, 6, 9, 12, 15, 18, 21]
  </code></pre>