Maybe I've just been staring at my screen too long today, but something I think should be very basic is stumping me.
I'm trying to make a 'copy' of a variable so I can manipulate it without modifying the original.
# original var is set
foo = ["a","b","c"]
# i want a copy of the original var so i dont modify the original
bar = foo
# modify the copied var
bar.delete("b")
# output the values
puts bar # outputs: ["a","c"] - this is right
puts foo # outputs: ["a","c"] - why is this also getting modified?
I want foo
not to be changed.
foo
and bar
refer to the same object. To make bar
refer to a different object, you have to clone foo
:
bar = foo.clone
You can use the dup method:
bar = foo.dup
This will return a copy of foo, so any changes you make to bar will not effect foo.
In the below code you can see that foo
and bar
both are having the same object_id
. Thus changing to the foo
instance causes same reflection to see through the 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 here also that I freeze the foo
in effect the bar
also has been frozen.
foo.freeze
#=> ["a", "c"]
foo.frozen?
#=> true
bar.frozen?
#=> true
Thus the right way is 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
Cheers!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With