From: nobu.nokada@... Date: 2002-12-19T10:28:18+09:00 Subject: Re: Hash.new{...} bug? Hi, At Wed, 18 Dec 2002 22:54:59 +0900, Bulat Ziganshin wrote: > >> hash = Hash.new{Hash.new} > nnsn> Hash 'default' proc never modify the hash itself automatically. > nnsn> Try with this. > > nnsn> hash = Hash.new {|h,k| h[k] = Hash.new} > > thanks. but with Hash.new(...) hash element updated to default value. > is this difference intended? In the first style you wrote, the returned value is updated but discarded soon. The last style, the default value is updated as same but kept in the hash and reused. `Default value' may need operator assignments to work well. h = Hash.new(0) h[0] += 1 h[:foo] += 2 p h # {0=>1, :foo=>2} class Hash def <<(arg) if Hash === arg update(arg) else store(*arg) end self end end h = Hash.new {{}} h[:foo] <<= [:bar, 1] h[:zot] <<= {:baz=>2} p h # {:zot=>{:baz=>2}, :foo=>{:bar=>1}} -- Nobu Nakada