给定以下代码:
class MagicList
def items=(array)
@items = array.map{|x| x*2}
end
def items
@items
end
end
list = MagicList.new
returns = list.items=([1, 2, 3])
puts returns.inspect # => [1, 2, 3]
puts list.items.inspect # => [2, 4, 6]
我预计 的值为returns
,[2, 4, 6]
因为两者@items
都array.map{|x| x*2}
返回此值。为什么呢[1, 2, 3]
?