From: Ashley Moran Date: 2006-12-10T08:57:01+09:00 Subject: Re: Help me with some nested Hashes. On Dec 09, 2006, at 11:32 pm, Daniel Finnie 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 [](key) 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