How to copy a ruby variable?
ruby, variables
Solution
`foo` and `bar` refer to the same object. To make `bar` refer to a different object, you have to clone `foo`:
bar = foo.clone
Problem
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.