From: Ian Macdonald Date: 2002-03-16T04:02:37+09:00 Subject: Re: new user confused by multidimensional Ruby hashes On Fri 15 Mar 2002 at 19:18:06 +0900, you wrote: > * Thomas Hurst (tom.hurst@clara.net) wrote: > > > * Ian Macdonald (ian@google.com) wrote: > > > > [ianmacd@caliban ~]$ irb > > > irb(main):001:0> foo = Hash.new( {} ) > > > {} > > > > Note: what you've actually done here is: > > > > irb(main):001:0> foo = Hash.new(Hash.new) > > {} > > > > Which is really just a funny one dimensional hash (you didn't specify a > > key for the second hash, so everything references it, oops): > > Er, my back> > > What you've actually done is specify a hash who's default value is > another hash object; that means unless you specify otherwise every hash > index references that same object. > > To have Hash.new evaluated to create a new object each time, you need to > pass it a block, but that's only supported in 1.7 (although you should > be able to override Hash#[] to provide this functionality if you really > need it), otherwise you need to do it manually. > > Hope that's a bit clearer :) Thank you very much. Your postings made things much clearer. Ian -- Ian Macdonald | The heart is not a logical organ. -- Dr. System Administrator | Janet Wallace, "The Deadly Years", stardate Google, Inc. | 3479.4 ian@google.com | 650.330.0100 x1265 |