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:
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.
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
···
2010/2/2 yermej <yermej@gmail.com>:
On Feb 1, 11:35 am, Jack Bauer <realmadrid2...@yahoo.es> 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:
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.