Episode #045: Hash Default Value

Upgrade to download episode video.

Episode Script

Back in Episode 32, we learned about passing a block to the Hash constructor in order to provide a default value for missing keys.

If you've ever taken a look through the Hash documentation, you might have noticed that a Hash can also take a default value as an argument as well as in the form of a block. And in fact, this form works just fine for our word count hash:

When we use the default value argument in other scenarios we run into trouble though. For instance, here's a hash whose default value for missing keys is an empty array. This lets us append values to hash members without explicitly initializing them.

But when we start adding values to more than one key, we discover a problem. All of our values are being appended to a single array!

If we give a name to our default value array, we can see what is happening more clearly: all of the values are being added to the single array that we passed in as the default value.

This happens because Hash uses the same default object everywhere. It doesn't duplicate it before use. By contrast, when we use a default block instead of a default value, the block is executed every time a default value is needed, thus generating a new Array object every time.

This behavior of Hash default values is one of those non-obvious gotchas that turns into a real head-scratcher the first time you run into it. I hope that by showing it to you today, I've saved you some debugging time down the road.

Until next episode, happy hacking!