Nested Iteration Workshop
Learning Goals
- Practice more complex iteration patterns
- Iterate over multiple collections
- Use
pry
to debug / as an implementation tool
Warmup
Given the following array:
animals = [:dog, :cat, :zebra, :quokka, :unicorn, :bear]
Use an enumerable to
- Return an array of animals as strings
- Return an array of animals with length >= 4
Given the following array:
nested_animals = [[:dog, :cat, :zebra], [:quokka, :unicorn, :bear]]
Use an enumerable to
- Return an unnested (single layer) array of animals as strings
- Return an unnested array of animals with length >= 4
- Return a hash where the keys are the animal, and the values are the length. ex:
{dog: 3, cat: 3, zebra: 5 ... }
Practice
We will be using the tools we have already learned (.each
, .map
, pry
, etc…) to tackle some more complex iterations. Up to now we have focused on iterating over a single collection to get some information, but you will often need to look at multiple collections to get the desired information. To help us practice, clone this repo. Follow the instructions in the README.
More Practice
You can clone this repo
In the repo, you will find 4 skipped tests. Starting with course_spec.rb
, write the implemenation code to make these tests pass. The first test will help familiarize you with the code base. After course_spec
, move on to the skipped tests in school_spec.rb
. We will be stopping and checking in as a group throughout the exercise.
As you work, use pry
OFTEN to verify your assumptions and try things out!