From: Jimi Damon Date: 2007-12-08T09:00:16+09:00 Subject: Re: Problem with Hash of Arrays Bernardo Rufino wrote: > 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"] Yes, but if you run this example and type "a" you get irb(main):003:0> a => {} I'm sorry...but I think this is incorrect... You have defined an array as being the default type , hence after you have performed << "test" , a should contain => {:first=>["test"]} But it does not.. As for the other post. When you define Hash.new( ) , this is the default value when you HAVE NOT defined the key for that value. Hence, if I do a = Hash.new("tmp") and type in irb irb(main):006 a = Hash.new("tmp") => {} irb(main):007 a[:first] => "tmp" irb(main):008 a => {} irb(main):009 This makes sense because it is accessing the default value for the key which is "tmp" However, with that being said, if my default type is an "array", then I should be able to Push into that array a value and have it stay around, other wise why does a[:key] += ["value"] and then a[:key].push("another value") work ? -- Posted via http://www.ruby-forum.com/.