From: Marnen Laibow-Koser Date: 2010-02-02T02:52:38+09:00 Subject: Re: Adding keys to a hash in a loop Jack Bauer wrote: > Robert Klemme wrote: > >> Erm, how would you put a key value pair into a Hash without a loop? > > This is the loop... > > output.each do |item| > output_hash << {item.name => []} > end > > Iterate through items in the "output" array and create a key for the > "output_hash" hash using the value of "item.name" as the key, and an > empty array as the value for that key. > > I know the output_hash << won't work, that's why I'm asking how I can go > about it. I was just using that as a sort of pseudo-code to get my point > across. The same way you assign to a hash element outside a loop: output_hash[item.name] = value . << is only useful with arrays, not hashes. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org -- Posted via http://www.ruby-forum.com/.