# Help me with some nested Hashes

**URL:** <https://www.ruby-forum.com/t/help-me-with-some-nested-hashes/78885>\
**Category:** Ruby\
**Created:** [December 10, 2006, 12:34am UTC](https://www.ruby-forum.com/t/help-me-with-some-nested-hashes/78885 "2006-12-10T00:34:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [December 10, 2006, 12:34am UTC](https://www.ruby-forum.com/t/help-me-with-some-nested-hashes/78885/1 "2006-12-10T00:34:21Z")

</div>

What I want to do is have 2 nested hashes, the outer hash returning a  
new hash on an unknown key and the inner hash returning a space on an  
unknown key.

For example:  
values = Hash.new # as described above  
values[4][4] = “H” # The 4 is an unknown key, so a new Hash is  
formed # and the value of that inner hash’s 4 key is “H”  
values[5][6] # =\> " "

I got this to work correctly with the following (only ints are keys):  
@value = []  
0.upto(300) do |x|  
@value[x] = Hash.new(" ")  
end  
But that is horrible.

So I’ve tried this:  
value = Hash.new(Hash.new(" "))  
But if you pass an object as the default value for a hash, it is not  
cloned for each unknown key.

So then this:  
value = Hash.new { Hash.new(" “) }  
But that has some problems as well:  
irb(main):013:0\> value = Hash.new {Hash.new(” ") }  
=\> {}  
irb(main):014:0\> value[5][6] = “H”  
=\> “H”  
irb(main):015:0\> value[5][6]  
=\> " "  
irb(main):016:0\> value[5][7] = “I”  
=\> “I”  
irb(main):017:0\> value[5][6]  
=\> " " # It just changed from what I set it to above.

Is there anyway to get this to work?

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [December 10, 2006, 12:38am UTC](https://www.ruby-forum.com/t/help-me-with-some-nested-hashes/78885/2 "2006-12-10T00:38:22Z")

</div>

Daniel F. wrote:

> What I want to do is have 2 nested hashes, the outer hash returning a  
> new hash on an unknown key and the inner hash returning a space on an  
> unknown key.

value = Hash.new {|h,k| h[k] = Hash.new {|h1,k1| h1[k1] = " "}}

p value[5][6] # ==\> " "  
value[5][6] = “H”  
p value # ==\> {5=\>{6=\>“H”}}

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [December 10, 2006, 12:46am UTC](https://www.ruby-forum.com/t/help-me-with-some-nested-hashes/78885/3 "2006-12-10T00:46:22Z")

</div>

Joel VanderWerf wrote:

> 

Another option, in case you don’t want all those empty strings  
cluttering up your hash:

value = Hash.new {|h,k| h[k] = Hash.new {" "}}

p value[5][6] # ==\> " "  
p value # ==\> {5=\>{}}

value[5][6] = “H”  
p value # ==\> {5=\>{6=\>“H”}}

# But, with this variation, beware:

value[5][7] \<\< “add some characters after the space”  
p value[5][7] # ==\> " "

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [December 10, 2006, 12:58am UTC](https://www.ruby-forum.com/t/help-me-with-some-nested-hashes/78885/4 "2006-12-10T00:58:23Z")

</div>

On Dec 09, 2006, at 11:32 pm, Daniel F. wrote:

> What I want to do is have 2 nested hashes, the outer hash returning  
> a new hash on an unknown key and the inner hash returning a space  
> on an unknown key.

Daniel

I was beat to it, but since I spent ten minutes figuring it out, here  
is my solution anyway 🙂

require ‘rubygems’  
require ‘spec’

# a subclass, instead of a re-opened base class… how un-rubyish 😉

class SpaceHash \< Hash  
def   
value = super(key)  
value = self[key] = Hash.new(" ") unless value  
value  
end  
end

context “SpaceHash” do  
setup do  
@space\_hash = SpaceHash.new  
@space\_hash[5][6] = “H”  
end

specify “unassigned keys return a space” do  
@space\_hash[5][5].should == " "  
end

specify “assigned keys should return their value” do  
@space\_hash[5][6].should == “H”  
end

specify “assigned another key to the second layer should not  
reassign othe keys” do  
@space\_hash[5][7] = “I”  
@space\_hash[5][6].should == “H”  
end  
end

Ashley

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [December 10, 2006, 12:52am UTC](https://www.ruby-forum.com/t/help-me-with-some-nested-hashes/78885/5 "2006-12-10T00:52:23Z")

</div>

Thanks, that works perfectly! (I’m only storing 1 char strings)
