RubyA trick with Ruby Hash.new
Originally published here
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
It just returns an empty hash whose unexisting keys return always
This is just equivalent to using an empty Hash literal also known as
It allows for a parameter which is returned in case the key doesn't exist.
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:
That is why only recommend using this option in a case: maps of classes.
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.
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.
My preferred use case for this is to protect myself from
nils and to avoid continuous
In a case like this:
You can either ensure the key is initialized
Or use the trick we just learned to ensure you will never have a
nil and you get a sane default instead.
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:
How to use it then? Just do not let the rest of the world know it is a Hash 😬
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.