You think that the way you create your hash, ensures, that each (yet
undefined) hash element springs into existence as an empty array.
However, this is not the case. Try out the following:
hash = Hash.new()
hash << 'x'
So far, nothing unusual. Now do a
You will see, that this ALSO outputs an x. Next, do a
hash << 'y'
You will see that this prints x and y. What happens is, that all hash
elements share the same array. This is because when Ruby copies the
element to create a new hash key, there is no “deep copy” (i.e. cloning)
involved. Only the array reference is copied.
Now to your question:
You might get a hint of what is going on, is if you try
This will print an empty list. No keys defined. Why? You had defined a
certain array to replace the value nil to denote “no value defined”.
Since there is no key which is different from this value, it means there
is no key defined at all. Hence you get an empty list when you print out