From: 7stud -- Date: 2007-11-04T06:11:47+09:00 Subject: Re: x=[:some_key] does not work? Jano Svitok wrote: > > x = Hash.new { Hash.new } > > This will add an "automatic" two level hash. I don't know quickly how > to make this indefinitly deep, you can at least repeat the pattern. > > Please note that it is not enough to write Hash.new { {} } as the > inner will create one particular Hash instance, that all keys will > reference. You need Hash.new to create a new hash for each key. > > Jano As far as I can tell neither of the following does anything: x = Hash.new { Hash.new } y = Hash.new{ {} } x[:bla][:some_key] = true y[:bla][:some_key] = true p x, y --output:-- {} {} From the docs for Hash.new: If a block is specified, it will be called with the hash object and the key, and should return the default value. It is the block's responsibility to store the value in the hash if required. Your lookup of a non-existent key returns a hash, but the returned hash never gets stored in the original hash, so the original hash remains empty. -- Posted via http://www.ruby-forum.com/.