DEV Community

Fran C. for Factorial

Posted on

A trick with Ruby Hash.new

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

Regular form

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

h = Hash.new # Or h = {}
h[:hello] # => nil
Enter fullscreen mode Exit fullscreen mode

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.

h = Hash.new('world')
h[:hello] # => 'world'
Enter fullscreen mode Exit fullscreen mode

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:

h[:hello].upcase! # "WORLD"
h[:foo] # "WORLD"
Enter fullscreen mode Exit fullscreen mode

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

POLICIES = Hash.new(ForbidAllPolicy).merge({
  admin: AccessAllPolicy,
  user: RestrictedPolicy
})

policy = POLICIES[current_user.role]

POLICIES[:user]
# => RestrictedPolicy

POLICIES[:hacker]
# => ForbidAllPolicy
Enter fullscreen mode Exit fullscreen mode

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.

h = Hash.new 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"
Enter fullscreen mode Exit fullscreen mode

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

In a case like this:

h = {}
h[:hello] << :world
# => NoMethodError (undefined method `<<' for nil:NilClass)
Enter fullscreen mode Exit fullscreen mode

You can either ensure the key is initialized

h = {}
h[:hello] ||= []
h[:hello] << :world
h
# => {:hello=>[:world]}
Enter fullscreen mode Exit fullscreen mode

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

h = Hash.new { |h, k| h[k] = [] }
h[:hello] << :world
h
# => {:hello=>[:world]}
Enter fullscreen mode Exit fullscreen mode

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:

h.keys
# => [:hello]
Enter fullscreen mode Exit fullscreen mode

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

class CachedUser
  def initialize
    @cache = Hash.new { |h, id| h[id] = User.find(k) }
  end

  def fetch(id)
    @cache[id]
  end
end

cache = CachedUser.new

cache.fetch(1)
# => select * from users where id=1
# => <User: @id=1>

cache.fetch(1)
# => <User: @id=1>
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)