From: Gary Wright Date: 2007-12-08T11:46:41+09:00 Subject: Re: Problem with Hash of Arrays On Dec 7, 2007, at 7:17 PM, Jimi Damon wrote: > However, really what I am looking for is a default constructore for > the > blank value. Bernardo already showed you: > In addition, to create different Arrays objects use a block... > > irb(main):001:0> a = Hash.new{|hash, key| hash[key] = Array.new;} > => {} > irb(main):002:0> a[:first] > => [] > irb(main):003:0> a[:first] << "test" > => ["test"] > irb(main):004:0> a[:sec] > => [] A block provided to Hash.new is called every time there is miss during key lookup. The two arguments to the block are the hash itself and the key that caused the missed lookup. If the code in the block stores a value into the hash with that key, then there won't be any future misses for that key. In the example that Bernardo showed, a new array is constructed every time a key lookup fails and then stored in the hash using that key. Subsequent lookups with that key get that array--that is to say, an new array is only created once per key. Gary Wright