I have two arrays
a = [:foo, :bar, :baz, :bof]
and
b = ["hello", "world", 1, 2]
I want to
{:foo => "hello", :bar => "world", :baz => 1, :bof => 2}
How to do it?
h = Hash[a.zip b] # => {:baz=>1, :bof=>2, :bar=>"world", :foo=>"hello"}
... Damn, I love Ruby.
Just wanted to point out that there is a slightly cleaner way to do this:
h = a.zip(b).to_h # => {:foo=>"hello", :bar=>"world", :baz=>1, :bof=>2}
I have to agree to the part "I love Ruby", though!
How about this?
[a, b].transpose.to_h
If you are using Ruby 1.9:
Hash[ [a, b].transpose ]
I feel that a.zip(b) looks like a is a master and b is a subordinate, but in this style they are flat.
a.zip(b)
a
b