Hash Defaults and Path of Madness

This is a little story about innocent Ruby mistakes and how they can ruin your day. One of our interns here are Powerset had some code that used default hash values.

1
2
h = {}
h.default = []

Fair enough. Then the code did this:


h[:foo] << 'foo' # => ["foo"]

Which happily returns in exactly the expected fashion. We can even check the value to make sure:


h[:foo] # => ["foo"]

But then later on we look more closely at the hash and WTF it's empty!


h # => {}

Madness! Until you realize that a hash's default value is only used as the return value for a hash lookup of a non-existent key.

1
2
h.default # => ["foo"]
h[nil] # => ["foo"]

And then you can only laugh once you look back at the code that seemed to work and see exactly why it was so deceiving.


About this entry