2

I need to create a new Hash object using two arrays.

But, the conditions is first array value should be a key value for the Hash and second array value should be the Hash value.

a = ["x", "y"] 
b = [2, 4]

Result should be: c = {"x" => 2, "y" => 4}

4

1 回答 1

12
irb(main):001:0> a = ["x", "y"]; b = [2, 4]
=> [2, 4]
irb(main):002:0> Hash[a.zip(b)]
=> {"x"=>2, "y"=>4}
于 2012-06-14T11:25:58.297 回答