使用两个数组创建一个散列。

时间:2021-06-06 12:54:30

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}

结果为:c = {"x" => 2, "y" => 4}

1 个解决方案

#1


11  

irb(main):001:0> a = ["x", "y"]; b = [2, 4]
=> [2, 4]
irb(main):002:0> Hash[a.zip(b)]
=> {"x"=>2, "y"=>4}

#1


11  

irb(main):001:0> a = ["x", "y"]; b = [2, 4]
=> [2, 4]
irb(main):002:0> Hash[a.zip(b)]
=> {"x"=>2, "y"=>4}