In the code below, you can see that foo and bar both have the same object_id . Thus, going to the foo instance causes the same reflection as through bar .
foo = ["a","b","c"] #=> ["a", "b", "c"] foo.object_id #=> 16653048 bar = foo #=> ["a", "b", "c"] bar.object_id #=> 16653048 bar.delete("b") #=> "b" puts bar #a #c #=> nil puts foo #a #c #=> nil
See also that I freeze foo , as a result the bar was also frozen.
foo.freeze #=> ["a", "c"] foo.frozen? #=> true bar.frozen? #=> true
So the correct way below:
bar = foo.dup #=> ["a", "b", "c"] foo.object_id #=> 16450548 bar.object_id #=> 16765512 bar.delete("b") #=> "b" print bar #["a", "c"]=> nil print foo #["a", "b", "c"]=> nil
Hooray!!
Arup rakshit
source share