class Foo
def bar
@instance_variable = [['first']]
# make a duplicate object with the :dup method
local_variable=@instance_variable.dup
# They have different object_id
p @instance_variable.object_id
p local_variable.object_id
local_variable.each{|n|n.push('second')}
@instance_variable
end
end
f=Foo.new
p f.bar
=> 2000
=> 2002
=> [["first", "second"]]
It seems that the local_variable still references to the @instance_variable, although it is a different object. This behaviour is both with the push and unshift in the each block. With a normal assignment like local_variable='second', the result is as expected => [['first']]
I don't understand why local_variable.each{|n|n.push('second')} has an effect on the @instance_variable
Using Ruby-1.9.2p318