From: Gennady Bystritsky Date: 2007-10-30T02:04:33+09:00 Subject: Re: Something wrong with my hash of hash list-bounce@example.com wrote: > I try to get a hash of hash to make some sort of counter (I have a set > of random pair and want to count the number of times they > appear). I use > this code but even if I can access data, my hash seems empty (so each > and the others don't work): > > > h = Hash.new(Hash.new(0)) > h["a"]["b"] += 1 > h["a"]["b"] += 1 > h["a"]["c"] += 1 > p h["a"]["b"] => 2 > p h["a"]["c"] => 1 > p h.class => Hash > p h["a"].class => Hash > p h["a"]["b"].class => Fixnum > p h => {} > > > What's wrong? > > Lars Try this definition: h = Hash.new { |h, k| h[k] = Hash.new(0) } It will actually modify 'h' rather than simply returning and modifying the default value. Gennady.