From: Daniel Finnie Date: 2006-12-10T08:51:37+09:00 Subject: Re: Help me with some nested Hashes. Thanks, that works perfectly! (I'm only storing 1 char strings) Joel VanderWerf wrote: > Joel VanderWerf wrote: >> 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. >> >> 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"}} >> > > 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] # ==> " " >