From: dblack@... Date: 2006-03-24T04:25:03+09:00 Subject: Re: questions/suggestions from a Ruby newbie Hi -- On Thu, 23 Mar 2006, Pit Capitain wrote: > dblack@wobblini.net schrieb: >> This reminds me of my favorite way to create an pseudo-auto-vivifying >> hash: >> >> Hash.new {|h,k| h[k] = h.dup } >> >> :-) I don't think there's an equally concise way for the array thing. > > David, is this really working for you? > > x = Hash.new {|h,k| h[k] = h.dup } > x[1] = "x1" > x[2] > p x # => {1=>"x1", 2=>{1=>"x1"}} > > Maybe you mean > > Hash.new {|h,k| h[k] = h.dup.clear } Oh, I knew someone would find a problem with it :-) I think that's right (the 'clear'). I'd only tested it, if it's worthy of the name test, with things like: x[1][2][3]. Even so, I think it's still the most concise implementation of this concept. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black