On Wed, Nov 4, 2009 at 9:22 PM, Aldric Giacomoni <aldric / trevoke.net> wrote:
> Aldric Giacomoni wrote:
>>
>> Upon inspection of 'a', it sure seems that it's empty, but upon
>> inspection of the key, it does have a value attached to it! Can we trust
>> this? Expected / unexpected behavior ?
> # Reminder : a = Hash.new(Hash.new)

The default argument to a hash gets *returned* when the key is not
found, but not added to the hash. The logic is more or less

def [](key)
  if self.contains(key):
    return self.value_of(key)
  else
    return default
end

What you want is the block version, which accepts the hash itself and
the key as parameters, and lets you do the right thing, here

a = Hash.new {|hash, key| hash[key] = Hash.new}

>> a = Hash.new {|hash, key| hash[key] = Hash.new}
=> {}
>> a[5]
=> {}
>> a
=> {5=>{}}

martin