Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby, how to create a hash from two arrays?

I'm fairly a beginner in Ruby and I'm trying to do the following: Let's say I have two arrays:

array_1 = ["NY", "SF", "NL", "SY"]
array_2 = ["apple", "banana"]

I want to merge the arrays to a hash so each object in array_1 will be assigned with the objects in array_2

Thanks in advance.

like image 422
Elad Kuzy Avatar asked Dec 20 '25 16:12

Elad Kuzy


1 Answers

x = [:foo, :bar, :baz]
y = [1, 2, 3]
x.zip(y).to_h # => {:foo=>1, :bar=>2, :baz=>3}
like image 183
ndnenkov Avatar answered Dec 22 '25 10:12

ndnenkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!