Q:

Hashes in hashes that produce default array

Suppose I have:
a = Hash.new
a['google.com'][:traffic] << 50
a['google.com'][:traffic] << 20
a['google.com'][:popular] << 1
a['google.com'][:popular] << 2
3a['yahoo.com'][:anythinghere] << 3

needs to produce something like this:
a = { 'google.com' => {traffic: [50,20], popular: [1,2]}, 'yahoo.com' => { anythinghere: [3,4] } }

So far I've tried something of this kind in hope that it would produce this result:
a= Hash.new(Hash.new(Array.new))

For example, a['google.com'] would produce a new hash while a['google.com'][:anythinghere] would produce a new array. When I try to execute the above insertions, however, a is empty? No idea what's going on, I'm pretty sure I'm missing something fundamental here. Take a look:
a = stats = Hash.new(Hash.new(Array.new))
a['google.com'][:traffic] << 5
a['google.com'][:traffic] << 6
p a['google.com'][:traffic] #=> [5,6]
p a['google.com'] #=> empty hash???
p a #=> empty hash???

A:

The reason why you're getting this unexpected behavior is because when you pass a default value to a Hash as an argument to new, that one object is the default value for all keys. For example:
s = "but"
a = Hash.new(s)
a['x'].concat 't'
puts a['y']
# butt
puts s
# butt

This makes sense since Ruby passes objects using pointers, so whenever you get the default value it's really a pointer to the original object you passed.
To work around this, you can set the default value in a block. That way the Hash has to reevaluate the block every time it needs the default value:
a = Hash.new { "but" }
a['x'].concat 't'
puts a['x']
# but
puts a.size
# 0

The next gotcha is that when Ruby gets the default value, it doesn't assign it to any keys. That's why the size of the Hash is still 0 after accessing a key in the previous example. This problem can also be worked around since Ruby supplies the hash itself and the missing key to the default value block, so we can do the assignment there:
a = Hash.new { |hash, key| hash[key] = "but" }
a['x'].concat 't'
puts a['x']
# butt
puts a['y']
# but
puts a.size
# 2

