January 17 - Generating a Hash from Two Arrays in Ruby
This coding exercise examines how to build a method that takes in two arrays and outputs a hash where one array makes up the keys and the other array makes up the values.
Guide Tasks
- Read Tutorial
- Watch Guide Video
- Complete the Exercise
Video locked
This video is viewable to users with a Bottega Bootcamp license
Already a Bottega Student? Sign In
Summary
Build a Hash generating method that creates a key/value Hash based on the data from two arrays.
Exercise Description
Build a method that takes in two arrays as arguments and returns a hash where one array makes up the keys and the other array makes up the values.
Sample Input
arr_1 = ['title', 'description', 'rating'] arr_2 = ['Fountainhead', 'Novel about unique perspectives', 5]
Sample Output
{"title"=>"Fountainhead", "description"=>"Novel about unique perspectives", "rating"=>5}
Real World Usage
A common practice in Ruby development is combining and mapping collections and converting input data into various data structures. This skill can be utilized when performing tasks such as mapping a set of headers to data values, much like you'd do when rendering CSV data in a web application.