TIL: Hash Edition
I was reviewing some code for a co-worker today and learned something
new about Hash
. My co-worker was using a ruby hash not as a
dictionary but as a O(1) look up collection (also known as a hash
table). Because of this there were a few places in his code that
looked something like this:
Whenever I see the same value being used for all the keys in a hash I think “use a hash with a default value”. I dutiful rewrote the code to look this like:
And then the tests started failing. Out of curiousity I tried this instead:
And the tests passed again. What the heck? So I went into irb and tried a couple things: kxx
Moral of the story: Keys are not added to a hash unless there is an
assigment or there is a default_proc. Also, if you want a collection
with O(1 ) key access use Set
. It is a hash under the hood but using
Set
reveals your intent.
P.S. For extra fun try this: