I’m trying to zip 3 arrays into a hash. The hash is coming up empty, though. Here’s sample code to reproduce using Ruby 1.9:
>> foo0 = ["a","b"]
=> ["a", "b"]
>> foo1 = ["c","d"]
=> ["c", "d"]
>> foo2 = ["e", "f"]
=> ["e", "f"]
>> h = Hash[foo0.zip(foo1, foo2)]
=> {}
I’d like to zip these and then do something like:
h.each_pair do |letter0, letter1, letter2|
# process letter0, letter1
end
It’s not clear what you expect the output to be but the
[]operator of theHashclass is intended to take an even number of arguments and return a new hash where each even numbered argument is the key for the corresponding odd numbered value.For example, if you introduce
foo3 = ["d"]and you want to get a hash like{"a"=>"b", "c"=>"d"}you could do the following: