Ruby and the Insuperable Enumerable

Liz Burke
5 min readFeb 18, 2021

--

A spirited guide to conceptualizing Ruby’s perplexing enumerable mixin.

stock image provided by pexel.com

From the title you might imagine that this is the tale of an aspirant hero, Ruby, dredging tirelessly into the unknown. Our protagonist marches onward, with a sole mission: to find the remedy for the disease plaguing the people of Matsumotoville. In reality, Ruby is a versatile object oriented programming language designed to mirror natural life, but what is an enumerable?

The enumerable module is a mixin (mix-in) that when combined with a collection class, allows users to traverse and search through collections of data. If I lost you on that last line, stay with me because I promise it will make sense in the end.

Going back to our metaphor, think of our hero Ruby. Ruby knows that within a dense jungle grows a special medicinal plant that would cure the illness plaguing the Matsumotians. The jungle, in this instance, is representative of a collection class, such as a hash or array. The jungle provides collections of plants, animals, and landscapes — the objects that define it. So how, in all of this, will our hero locate and deliver (return) these life saving plants to the people? *cue action music* Only with the help, of a highly trained team of searchers, or as we will come to know them: The Enumerable!

Before deciding which enumerable to use, think in detail about exactly what you are wanting to do with a collection of data. In this instance Ruby needs to traverse the jungle to find multiple seedlings of the “aloe variable” plants to bring back to Matsumotoville. They must be fully in tact, unaltered, and they must seedlings. Ruby has never even seen the “aloe variable” so it’s time to get help from the Enumerable.

First Ruby meets Find. Find tells Ruby that she has the top half of a torn photo of the aloe variable and can help gather a sample. However, Find only has enough time to help Ruby obtain one sample of the aloe variable. If she had time to find_all, she would be ready to take on the challenge, but right now she just can’t give her all. One sample won’t be enough, so Ruby thanked Find for her honesty and went on to meet some of the other Enumerable crew.

Find Example

Next Ruby meets twins Map and Collect. Map and Collect are quite comfortable traversing the jungle and tell Ruby that they can identify the aloe variable.. but not in the seedling form, as they have never seen it, and have nothing to compare it to. They would need to gather samples of all the seedling plants to grow them into the adult plant first, or at least test the samples in some way, to be sure. Ruby knows that the aloe variable must be seedlings and cannot be altered in any way. Map and Collect need to do something to every sample so this won’t work. Ruby thanks the twins and moves on.

Map/Collect Example

Ruby sees Each standing across the room and tries for several minutes to get his attention. Meanwhile, Each walks up to every person introducing himself until he finally crosses paths with Ruby. Ruby asks if Each can help in this quest, to which Each replies: “I know what every plant in the jungle is, and I’ll tell you what each one is!” Ruby considers this option, but realizes that what Each is really good for is talking. Although the samples wouldn’t be altered if Each came along, it would likely take another person to complete the job, even if Each could make the journey. Onward goes our search.

Example of Each (still audible from a distance away)

Finally, Ruby meets Select. Select claims that she can in fact identify the aloe variable by comparing it to an illustration in an ancient book. She also explains that she knows the plant so well that she will select only the plants that Ruby is looking for and no other plants by mistake. She doesn’t need to alter the collected samples to return with the correct samples either. With this information Ruby chooses Select to join on the quest to save the people of Matsumotoville!

Example of Select

I’m going to end the story here and close out with a recap of each Enumerable. I hope that this guide has helped to conceptualize the capabilities of the enumberble mixin we met, and that you can embrace your journey of learning enumerables!

Ruby en route to save Matsumotoville (stock image c/o pexel.com)

Find: uses a comparison to return the value for the first (only one) item that fills the condition (evaluates to true).

Map/Collect: passes over each element in a collection changing the return value of each element to create a new array, without using a comparison.

Each: passes through a collection performing the same operation (from the block) to every element in a collection. It will return all of the elements in the original array values unchanged.

Select: passes through a collection by comparing to the block. Returns only the elements that translate as true or meet the specifications of the block.

--

--

Liz Burke

Full-time SE student at Flatiron. Exp. graduation June 2021. BA from Ball State University in Communications, Audio and Video Production