From: "David A. Black" Date: 2009-01-10T23:53:49+09:00 Subject: Re: Default values of hashes Hi -- On Wed, 7 Jan 2009, Glenn wrote: > Thanks for the tips. I learned a lot from all of the suggestions. > > Not sure, though, if I understand this line: > > hash[1][2][3] = Hash.new(0) > > Is that a valid line on its own. If so, it does not seem to work for me. I get the following error: > > undefined method `[]=' for 0 Here's how to use it: >> hproc = lambda {|h,k| h[k] = Hash.new(&hproc) } => # >> hash = Hash.new(&hproc) => {} >> hash[1][2][3] = Hash.new(0) => {} >> hash[1][2][3]["x"] += 10 => 10 >> hash[1][2][3] => {"x"=>10} 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!