I was recently tripped up when I was trying to use the ruby hash value defaults. I wanted the default value of my hash to be an empty hash. That way I could do something like the following, without worrying about initializing the hashes:
What I didn’t realise is that when you set a default value in a ruby hash, that value is a single object.
This means that the actual behaviour I experienced was pretty weird.
So here we have a hash within a hash.
The strangest part is that, as you see, if you assign directly to the child hash without explicitly assigning the value of a key in the parent, no key is set on the parent!
In effect, we’ve hidden data within the hash.
If you want the behaviour I mentioned in the first place, you have to initialize your hash by passing a block to it.