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
You’re currently reading “Hash Defaults and Path of Madness,” an entry on Ruby is Awesome

- Published:
- September 11th 03:52 PM
- Updated:
- September 11th 03:55 PM
- Sections:
3 comments
Jump to comment form | comments rss [?]