Lars Ticot wrote:
>> h = Hash.new(Hash.new(0))
> Changing declaration by
> h = Hash.new {|h,k| h[k] = Hash.new(0) }
> makes things ok. What are the difference beetween the two declaration?

There are two differences:
First: The first version executes "Hash.new(0)" once (before the outer 
Hash.new is called, since parameters are evaluated before the actual method 
call (obviously)) and stores the result as the default value. This means that 
if you do something like h[:foo][:chunky] = "bacon"  and h[:foo] has not been 
assigned before, you will actually change the default value, so h[:bar]
[:chunky] will also be "bacon". The second version just stores the block and 
executes it everytime a key that hasn't been assigned before is accessed.

Second: The second version has "h[k] =" in it, so it will not only return the 
newly created hash, it will also store it in the outer hash. The first 
version won't do that, which means that h will always appear as empty until 
you do any actual assignments to it.


HTH,
Sebastian
-- 
Jabber: sepp2k / jabber.org
ICQ: 205544826