RubyA trick with Ruby

Fran C.

Fran C.

3 minutes read

Originally published here

Hashes are used a lot in Ruby (sometimes even abused) and they have a very interesting functionality that is rarely used: has 3 different forms

Regular form

It just returns an empty hash whose unexisting keys return always nil.

1 2 h = # Or h = {} h[:hello] # => nil

This is just equivalent to using an empty Hash literal also known as {}.

Fixed default

It allows for a parameter which is returned in case the key doesn't exist.

1 2 h ='world') h[:hello] # => 'world'

This form needs to be used carefully though since you always return the same object, which means if you modify it, you modify it for all subsequent calls:

1 2 h[:hello].upcase! # "WORLD" h[:foo] # "WORLD"

That is why only recommend using this option in a case: maps of classes.

1 2 3 4 5 6 7 8 9 10 11 12 POLICIES ={ admin: AccessAllPolicy, user: RestrictedPolicy }) policy = POLICIES[current_user.role] POLICIES[:user] # => RestrictedPolicy POLICIES[:hacker] # => ForbidAllPolicy

Why classes but not other objects? Because classes are singletons (a singleton is an object that only has one instance at the same time) so you do not care that you're always going to get the very same object all the time.

Calculated default

This form gives the biggest freedom since it allows us to pass a block to calculate the value and even to store it on the hash. So the next call to that same key will already have a value.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 h = do |hash, key| value = key.upcase puts "'#{key}' => '#{value}'" hash[key] = value end h["hello"] # prints -> 'hello' => 'HELLO' # => "HELLO # Next call to the same key is already assigned, the block isn't executed h["hello"] # => "HELLO"

My preferred use case for this is to protect myself from nils and to avoid continuous nil checks.

In a case like this:

1 2 3 h = {} h[:hello] << :world # => NoMethodError (undefined method `<<' for nil:NilClass)

You can either ensure the key is initialized

1 2 3 4 5 h = {} h[:hello] ||= [] h[:hello] << :world h # => {:hello=>[:world]}

Or use the trick we just learned to ensure you will never have a nil and you get a sane default instead.

1 2 3 4 h = { |h, k| h[k] = [] } h[:hello] << :world h # => {:hello=>[:world]}

Take into account that passing around a Hash like this might be dangerous as well. Nobody will expect that a Hash returns something for a key that doesn't exist, it can be confusing and hard to debug.

If we get keys for the previous hash:

1 2 h.keys # => [:hello]

How to use it then? Just do not let the rest of the world know it is a Hash 😬

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class CachedUser def initialize @cache = { |h, id| h[id] = User.find(k) } end def fetch(id) @cache[id] end end cache = cache.fetch(1) # => select * from users where id=1 # => <User: @id=1> cache.fetch(1) # => <User: @id=1>

Although the example is extremely simple it showcases how you can safely use a Hash as a container object safely without exposing some of its drawbacks but profiting from its flexibility.

Fran C.
Staff Software Engineer

I break code for a living... wait no, I fix it, most of the time. Well, sometimes. Yeah, I break code for a living.

Liked this read?Join our team of lovely humans

We're looking for outstanding people like you to become part of our team. Do you like shipping code that adds value on a daily basis while working closely with an amazing bunch of people?

Made with ❤️ by Factorial's Team