From: Robert Klemme Date: 2010-02-02T16:56:43+09:00 Subject: Re: Adding keys to a hash in a loop 2010/2/2 yermej : > On Feb 1, 11:35 am, Jack Bauer wrote: >> Hi all, >> >> I'm trying to essentially accomplish this behavior: >> >> output.each do |item| >>   output_hash << {item.name => []} >> end > > Depending on how you'll be using output_hash, you might use a default > value instead of individually initializing the values. Based on your > sample, you can likely use: > > output_hash = Hash.new {|hash, key| hash[key] = Array.new} > > I'm guessing this is what Robert was hinting at. Actually, no. :-) But you are right, this is certainly an option in many cases. It all depends on the situation and we do know nothing about the "surrounding" code. I rather tried to help Jack solve this for himself since it's not too tricky. Providing the full solution is not always the best choice IMHO. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/