Given the following code:
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]
I expected the value of returns to be [2, 4, 6], because @items as well as array.map{|x| x*2} both return this value. Why is it [1, 2, 3]?
Array#mapdoesn't change the original variable;Array#map!changes the original variable.return @itemstodef items=it still returns the original array.