Tim Pease
1/16/2009 8:32:00 PM
On Jan 16, 2009, at 1:06 PM, Jason Lillywhite wrote:
> Why is it that if I create an array, a = [1,2,3]
> then do b = a
> then b[0] = 99
>
> why is that a[0] magically becomes 99 too?
>
a = [1,2,3]
a.object_id #=> 604244548
b = a
b.object_id #=> 604244548
Your two variables are referring to the same object ... or another way
of saying that is all variables in ruby hold a reference to an object.
To get a copy of your array ...
b = a.dup
b.object_id #=> 604262918
Welcome to ruby!
Blessings,
TwP