Ruby’s Hash#new
method accepts an optional parameter which is
returned when you try to access a key that does not exist in the hash (the default is nil). For example:
1 2 

While the first example may not very useful, Hash#new
also accepts a
block that will be called with the hash object and the key whenever you try to access
a key that does not exist in the hash, for example:
1 2 

You can also modify the hash within the callback. Here’s a more useful example where we create a memoized version of the fibonacci sequence:
1 2 3 4 5 6 7 8 9 10 

Using this hash, whenever we compute the nth fibonacci number all the n – 1 fibonacci numbers will be cached in the hash, significantly reducing the number of recursive calls needed to compute the next fibonacci number.
1 2 
