# How to create hash of hash?

**URL:** <https://www.ruby-forum.com/t/how-to-create-hash-of-hash/148891>\
**Category:** Rails\
**Created:** [September 18, 2008, 2:28am UTC](https://www.ruby-forum.com/t/how-to-create-hash-of-hash/148891 "2008-09-18T02:28:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdunneuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jdunneuk](https://www.ruby-forum.com/u/jdunneuk)\
**Post date:** [September 18, 2008, 2:28am UTC](https://www.ruby-forum.com/t/how-to-create-hash-of-hash/148891/1 "2008-09-18T02:28:45Z")

</div>

hashx = { key1 =\> {key1 =\> key1val1, key2 =\> key1val2}, key2 =\> {key1 =\>  
key2val1, key2 =\> key2val2} }

If I want to insert a hash in the 1st level, how to do?

key3\_hash = {key1 =\> key3val1, key2 =\> key3val2}  
hashx[key3] = key3\_hash

# Is there a better way to write this? I want to assign the

programmatically

How about adding to the 2nd level hash?

Say  
anon\_hash = hashx[key1]  
anon\_hash[key3] = {key3 =\> key1val3}  
hashx[key1] = anon\_hash

# is there a better way to do this?

---

<div class="post-metadata">

**Author:** ![jdunneuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jdunneuk](https://www.ruby-forum.com/u/jdunneuk)\
**Post date:** [September 18, 2008, 9:09am UTC](https://www.ruby-forum.com/t/how-to-create-hash-of-hash/148891/2 "2008-09-18T09:09:01Z")

</div>

On Sep 18, 1:28 am, John Do [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hashx = { key1 =\> {key1 =\> key1val1, key2 =\> key1val2}, key2 =\> {key1 =\>  
> key2val1, key2 =\> key2val2} }
> 
> If I want to insert a hash in the 1st level, how to do?
> 
> key3\_hash = {key1 =\> key3val1, key2 =\> key3val2}  
> hashx[key3] = key3\_hash
> 
> # Is there a better way to write this? I want to assign the
> 
> programmatically

Not quite sure what you’re getting at, but what’s wrong with  
hashx[key3] = {key1 =\> key3val1, key2 =\> key3val2} ?

> How about adding to the 2nd level hash?
> 
> Say  
> anon\_hash = hashx[key1]  
> anon\_hash[key3] = {key3 =\> key1val3}  
> hashx[key1] = anon\_hash
> 
> # is there a better way to do this?

why not just hashx[key1][key3]=key1val3 ? You don’t need all those  
intermediate steps.

Fred

---

<div class="post-metadata">

**Author:** ![jdunneuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jdunneuk](https://www.ruby-forum.com/u/jdunneuk)\
**Post date:** [September 18, 2008, 10:30am UTC](https://www.ruby-forum.com/t/how-to-create-hash-of-hash/148891/3 "2008-09-18T10:30:31Z")

</div>

On Sep 18, 3:08 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> why not just hashx[key1][key3]=key1val3 ? You don’t need all those  
> intermediate steps.
> 
> Fred

Maybe he’s dealing with really large hashes - or just many operations  
on them - that having to reference the first sub-hash via the parent  
hash every time would give a significant degradation in performance. I  
still prefer the approach you mentioned, though.

---

<div class="post-metadata">

**Author:** ![jdunneuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jdunneuk](https://www.ruby-forum.com/u/jdunneuk)\
**Post date:** [September 18, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/how-to-create-hash-of-hash/148891/4 "2008-09-18T20:56:45Z")

</div>

On Sep 18, 6:08 pm, John Do [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Erol F. wrote:
> 
> > On Sep 18, 3:08 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > wrote:
> 
> hashx[key1][key3] = key1val3 will give an error if hashx[key1] is null.  
> This is how I did it in perl, but ruby isn’t as forgiving.

hash = Hash.new {|h,k| h[k]=Hash.new &h.default\_proc}

Mwahahahahaha

Fred

---

<div class="post-metadata">

**Author:** ![jdunneuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jdunneuk](https://www.ruby-forum.com/u/jdunneuk)\
**Post date:** [September 18, 2008, 7:08pm UTC](https://www.ruby-forum.com/t/how-to-create-hash-of-hash/148891/5 "2008-09-18T19:08:35Z")

</div>

Erol F. wrote:

> On Sep 18, 3:08ï¿½pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > why not just hashx[key1][key3]=key1val3 ? You don’t need all those  
> > intermediate steps.
> > 
> > Fred
> 
> Maybe he’s dealing with really large hashes - or just many operations  
> on them - that having to reference the first sub-hash via the parent  
> hash every time would give a significant degradation in performance. I  
> still prefer the approach you mentioned, though.

hashx[key1][key3] = key1val3 will give an error if hashx[key1] is null.  
This is how I did it in perl, but ruby isn’t as forgiving.

Also, I want to be able to build an arbitrarily deep hash. Say  
hashx[key1][key11][key111]…[key11111].

The end goal is to build a hash that I can build an YUI treeview on. So  
I need to build the tree, then loop through the keys to create the  
TreeNode.
