From: "David A. Black" Date: 2009-01-04T22:37:06+09:00 Subject: Re: Default values of hashes Hi -- On Sun, 4 Jan 2009, Glenn wrote: > I actually have a followup question to this one. It turns out that sometimes I will need to go more than two levels deep with the hashes. I inferred that to go three levels deep, the code would be: h = Hash.new{|h,k| h[k]= Hash.new{|h2,k2| h2[k2]= Hash.new{|h3,k3| h3[k3]=0}}} This works fine. It seems, though, that I might have to go more levels deep than this. I am trying to look for a way to do this dynamically within a method. In other words, if I need 3 levels, it creates a hash within hash object 3 levels deep (like in the line of code above) and if I need 5, it creates one 5 levels deep. I do not know how to do this (or even if it is possible). Any suggestions on this one? It's come up before; here's the solution I seem to recall: hproc = lambda {|h,k| h[k] = Hash.new(&hproc) } hash = Hash.new(&hproc) hash[1][2][3] = 4 p hash # {1=>{2=>{3=>4}}} David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) http://www.wishsight.com => Independent, social wishlist management!