January 7 - Extract a Random Element from a Collection of Arrays Nested in a Hash
This coding exercise walks through how to work with the Hash data structure in Ruby. Specifically, you will learn how to select a random element from a set of nested arrays inside of a hash.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
  • Complete the Exercise
Video locked
This video is viewable to users with a Bottega Bootcamp license

Summary

Build a method that takes a hash of nested arrays as an argument and outputs a random, single element.

Exercise Description

Given the following hash:

menu = {
  'appetizers': ['Chips', 'Quesadillas', 'Flatbread'],
  'entrees': ['Steak', 'Chicken', 'Lobster'],
  'dessers': ['Cheesecake', 'Cake', 'Cupcake']
}

Build a method that returns a different random element each time it is processed.

Sample Output

"Chicken"

Real World Usage

Working with the Hash data structure is a common requirement, especially when building out Rails applications. In this exercise you'll learn how to work with nested collections and how to select elements from multiple collections.

Test Cases

Code File