vendredi 12 novembre 2021

How to maintain a copy of a hash with original values after changing some of its values in ruby?

I have tried to phrase this to the best of my ability. I have a hash that I perform some operations on but before I do this, I store this hash in another variable. Now when I access this variable, the values seem to have changed, how can I go around it. Example:

hash = {a: "1", b: "2", c: "3"}
hash_copy = hash
hash["a"]=4
puts(hash_copy["a"]) #prints 4 instead of 1

How can I get the put statement to print 1 instead of 4, that is, print the original value.

Aucun commentaire:

Enregistrer un commentaire