Andrew
Andrew

Reputation: 238667

Map an array to an array

I have an array of data and an array of keys that are in a specific order:

original_data = ['hello', 'world', 'one', 'two']
keys = [:greeting, :location, :first, :second]

I would like to map the keys to the data to be able to reference each datum by a key like this.

data[:greeting].should == original_data[0]

How can I achieve this?

Upvotes: 0

Views: 40

Answers (1)

pguardiario
pguardiario

Reputation: 54984

That would be:

data = Hash[keys.zip original_data]

Upvotes: 1

Related Questions