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]?
Because Ruby assignments always return the item they were passed, regardless of what the
setter=method returns.See also Is it possible to have class.property = x return something other than x?