Infinite hash and default_proc

Neeraj Singh

Neeraj Singh

December 31, 2010

I you already know how this infinite hash works then you are all set. If not read along.

Default value of Hash

If I want a hash to have a default value then that's easy.

1h = Hash.new(0)
2puts h['usa'] #=> 0

Above code will give me a fixed value if key is not found. If I want dynamic value then I can use block form.

1h = Hash.new{|h,k| h[k] = k.upcase}
2puts h['usa'] #=> USA
3puts h['india'] #=> INDIA

Default value is hash

If I want the default value to be a hash then it seems easy but it falls apart soon.

1h = Hash.new{|h,k| h[k] = {} }
2puts h['usa'].inspect #=> {}
3puts h['usa']['ny'].inspect #=> nil
4puts h['usa']['ny']['nyc'].inspect #=> NoMethodError: undefined method `[]' for nil:NilClass

In the above if a key is missing for h then it returns a hash. However that returned hash is an ordinary hash which does not have a capability of returning another hash if a key is missing.

This is where default_proc comes into picture. hash.default_proc returns the block which was passed to Hash.new .

1h = Hash.new{|h,k| Hash.new(&h.default_proc)}
2puts h['usa']['ny']['nyc'].inspect #=> {}

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.