From: Thomas Hurst Date: 2002-03-15T19:18:06+09:00 Subject: Re: new user confused by multidimensional Ruby hashes * 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, 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 :) -- Thomas 'Freaky' Hurst - freaky@aagh.net - http://www.aagh.net/ - Honesty pays, but it doesn't seem to pay enough to suit some people. -- F.M. Hubbard