It's impossible. Here's what happens:
hash[1] = "foo"
Note that the hash[1]
assignment does not change the object to which it refers, but rather simply changes the object that it refers to.
In Ruby (like many higher-level languages) you have no pointers and no explicit ability to manipulate links.
However, there are some methods that change, and on String one such example is upcase!
. In this example, we see that this method modifies the actual object being referenced without assigning a new object (and therefore the links remain unchanged):
hash[1] = "foo" #=> "foo" hash[2] = hash[1] #=> "foo" hash[2].upcase! #=> "FOO" hash # => {1=>"FOO", 2=>"FOO"}
source share