我正在尝试在对象上实现就地方法,但出现以下错误:
Can't change the value of self (SyntaxError)
当试图做
self = map(&block)
在以下对象上
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(&block)
res = Array.new
res << yield(value) if value
res << left.map(&block) if left
res << right.map(&block) if right
res.flatten
end
def map!(&block)
self = self.map(&block)
end
def to_a
map { |a| a }
end
end
我也尝试过使用 Enumerable 的一些破坏性方法无济于事
map(&block).collect!
我的方法有什么问题,您将如何实现这样的功能?
更新
为了澄清这个想法是在二叉树上实现映射,上面的映射方法成功地做到了,我的问题是将该方法转换为就地版本。
irb(main):001:0> require './node.rb'
=> true
irb(main):002:0> root = @root = Node.new(1, Node.new(2, Node.new(3), Node.new(4)),Node.new(5, Node.new(6), Node.new(7)))
=> #<Node:0x78803f58 @value=1, @left=#<Node:0x78abc090 @value=2, @left=#<Node:0x78abc0f0 @value=3, @left=nil, @right=nil>, @right=#<Node:0x78abc0c0 @value=4, @left=nil, @right=nil>>, @right=#<Node:0x78803f88 @value=5, @left=#<Node:0x78abc060 @value=6, @left=nil, @right=nil>, @right=#<Node:0x78abc030 @value=7, @left=nil, @right=nil>>>
irb(main):003:0> root.map { |a| a * 3 }
=> [3, 6, 9, 12, 15, 18, 21]
irb(main):004:0> root
=> #<Node:0x778fb828 @value=1, @left=#<Node:0x778fb9c0 @value=2, @left=#<Node:0x778fba68 @value=3, @left=nil, @right=nil>, @right=#<Node:0x778fb9f0 @value=4, @left=nil, @right=nil>>, @right=#<Node:0x778fb888 @value=5, @left=#<Node:0x778fb930 @value=6, @left=nil, @right=nil>, @right=#<Node:0x778fb8b8 @value=7, @left=nil, @right=nil>>>
irb(main):005:0> root.map! { |a| a * 3 }
=> [3, 6, 9, 12, 15, 18, 21]]
irb(main):006:0> root
=> [3, 6, 9, 12, 15, 18, 21]