---
title: "Infinite hash and default_proc"
description:
  "This blog discusses how we can build infinite nested hash in ruby using
  deafult_proc feature."
canonical_url: "https://www.bigbinary.com/blog/default_proc_in_infinite_hash"
markdown_url: "https://www.bigbinary.com/blog/default_proc_in_infinite_hash.md"
---

# Infinite hash and default_proc

This blog discusses how we can build infinite nested hash in ruby using
deafult_proc feature.

- Author: Neeraj Singh
- Published: December 31, 2010
- Categories: Ruby

I you already know how
[this infinite hash](http://twitter.com/#!/tenderlove/status/5687291469107200)
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.

```ruby
h = Hash.new(0)
puts 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.

```ruby
h = Hash.new{|h,k| h[k] = k.upcase}
puts h['usa'] #=> USA
puts 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.

```ruby
h = Hash.new{|h,k| h[k] = {} }
puts h['usa'].inspect #=> {}
puts h['usa']['ny'].inspect #=> nil
puts 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](http://ruby-doc.org/core-1.8.6/classes/Hash.html#M002854)
returns the block which was passed to `Hash.new` .

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

## Links

- [Human page](https://www.bigbinary.com/blog/default_proc_in_infinite_hash)
