Ruby: how can I copy a variable without pointing to the same object?
As for copying you can do:
phrase2 = phrase1.dup
or
# Clone: copies singleton methods as well
phrase2 = phrase1.clone
You can do this as well to avoid copying at all:
phrase2 = phrase1.gsub("Hello","Hi")
Using your example, instead of:
phrase2 = phrase1
Try:
phrase2 = phrase1.dup
phrase1 = "Hello Jim"
# => "Hello Jim"
phrase2 = Marshal.load(Marshal.dump(phrase1))
# => "Hello Jim"
phrase1.gsub!("Hello","Hi")
# => "Hi Jim"
puts phrase2
# "Hello Jim"
puts phrase1
# "Hi Jim"