I've come across the following concern in Ruby
a = [1, 2, 3]
b = a
b.delete_at(1)
b => [1,3]
a => [1,3]
b.object_id => 70178446287080
a.object_id => 70178446287080
So I kind of have an understanding of this. a
holds a reference to an array at the object_id
.
b
also has a reference to that SAME position as b
points to a
which refers to its object_id
. Basically they refer to the same thing. So if I mutate something for b
, a
is also mutated.
What category does this behavior fall in? Is there any readings/general practices that I can memorize so I won't have any errors in the future involving this? I know that a.dup
would provide a new object at a different location so a.dup == b
would be true
. Also for a.dup.object_id == b.object_id
.
Also, is dup
and clone
essentially the same thing in this situation, regardless of shallow vs deep?