Hi,


I have a loop in which I'd like to populate a Hash by pushing back new
elements to Arrays.

The problem is when I try to push back an element, because the default
element "references" the default, which is an empty array, the action is
only to make the default enlarges, not what I want :-).


irb> h = Hash.new([])
{}
irb> h["banana"].push(1)
[1]
irb> h
{}
irb> h.default
[1]
irb> h["apple"].push(2)
[1, 2]
irb> h
{}
irb> h.default
[1, 2]


Of course I wanted to end up with:

irb> h
{"apple"=>[2], "banana"=>[1]}


Does someone has the magical solution? Apart of course using nil (or
anything) as default value, and testing for existence of key before each
pushing, possibly allocating a new Array.

As far as I looked, the default value must be an object, it can't be set
to something callable (proc) each time we need the default.




-- 
Guillaume Cottenceau - http://mandrakesoft.com/~gc/