Choose Your Character: Ruby Iterator Edition

Programming languages have many ways to repeat instructions. While others use loops, Ruby leverages iterators to accomplish this task. Deciding what iterator to use can be tricky, but I’m here to help! First, let’s explore some basic terminology.

Loop

A Loop is a repetitive execution of a block of code; specified once and run until a specific condition is met. This action is particularly useful when you want to automate some repeatable action. An example of a loop would be:

Iterator

An iterator is an object (or method) allowing us to iterate over a dataset (particularly arrays and hashes). To iterate over a dataset means that we can go through each object in a collection and do some repeatable action. Although similar, the defining difference between loops and iterators is that a loop needs an external object to work (e.g., x=0), whereas an iterator belongs a (or is a built in method for) a collection. An example of an iterator would be:

Now that we understand the basic terminology, we can now dive into the main types of iterators in Ruby.

Filter/Select

Two names for the same iterator! This iterator allows us to select specific elements from a collection based upon some condition. For example:

This is particularly helpful when you want to select a specific group of elements in a collection; you’re not interested in the whole collection, just a subset of it. Find_all behaves in the same way.

Map/Collect

Again, two terms for the same iterator. Map/Collect will return a new object based upon specified calculations; or in other words, map/collect will create a new collection. For example,

It’s important to note that the initial array did not change. We can use Map/Collect to generate a new collection based on an existing collection, but we won’t change the initial object.

Find

Find will return the first element that fulfills some specified condition (i.e., the result of this iteration will be a single element, not a collection). For example:

Note: Include? is a string class method in Ruby which is used to return true if the given string contains a specified string or character.

Each

Each allows us to easily perform operations on elements in a collection. This iterator is also useful for when we just want to output some data to the console. For example:

It’s important to note that Each will not change the initial object; Each will always return the base, or original, collection.

All?/Any?

These iterators will return a boolean value after checking if a condition if met. All? will return true when all elements in a collection fulfill the condition, whereas Any? will return true if just one element in a collection fulfills the condition. For example:

Times

This iterator allows us to repeat tasks many times. Times is called on numbers (rather than a collection) and will return the numerical value of execution (instead of an original collection like Each). For example:

Graduate of Flatiron School’s Software Engineering Bootcamp as of 12/4/2020. Actively job searching in New York City.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store