Ruby Coding Exercise: 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.


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

Exercise File

Code File

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


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.


Can be found on the solutions branch on github.


Please enter your comment!
Please enter your name here