Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two arrays into a hash

Tags:

arrays

ruby

hash

I'm trying to combine two arrays into a hash.

@sample_array = ["one", "Two", "Three"]
@timesheet_id_array = ["96", "97", "98"]

I want to output the results into a hash called @hash_array. Is there a simple way to combine the two in a code block so that if you call puts at the end it looks like this in the console

{"one" => "96", "Two" => "97", "Three" => "98"}

I think this could be done in one or two lines of code.

like image 274
RubyDude1012 Avatar asked Nov 26 '22 21:11

RubyDude1012


1 Answers

try this

keys = [1, 2, 3]
values = ['a', 'b', 'c']
Hash[keys.zip(values)]

thanks

like image 188
Paritosh Singh Avatar answered Dec 19 '22 07:12

Paritosh Singh