From: Dave Thomas Date: 2002-03-27T23:21:56+09:00 Subject: Re: Hash.new(Hash.new) doesn't use Hash.new as default value "Jonas Delfs" writes: > In the documentation for the 'new' method in the Hash class in Programming > Ruby it says that 'anObject' will be used as default value, but consider the > following example: > > var = Hash.new(Hash.new) > var['index1'] = 'value1' > var['index2']['index22'] = 'value22' > var['index3']['index32'] = 'value32' > var['index4'] = 'value4' > p var['donotexist'] > > Why does "p var['donotexist']" output {"index32" => "value32", "index22" => > "value22"} - and _not_ just {} (a new Hash)? > If it's no bug, I think it's a bug in documentation. - and not a very logic > behavior. The default value is the hash created by Hash.new. There's just one of these, which is the default value for all entries. As a result, var['index2], var['index3'], and val['dontexist'] share it, which is the effect you're seeing. Dave