Ruby and Enumerables

Logan McGuire
4 min readAug 20, 2020
Old fashioned light bulb, with flecks of diffuse gold light on a black background.

If you have worked in Ruby then you have likely been to Ruby-Doc. It’s great, wonderful explains everything you could want to know about how that enumerable works. Of course, I had no idea what made the enumerables so special, why you would want each one under the hood? How does that enumerable work with variables which are not [a, b, c] or [1, 2, 3]?

First let’s set the scope for this post. Which enumerables get the most use and when should you use them? With those bite sized ideas, let’s get started.

.each, .map, .find/find_all, .reduce

Understanding the proper use of all these enumerables will put you on the right path to coding in Ruby. While by no means the only enumerables you should familiarize yourself with, these tools make the foundation from which the others can be most easily understood. Honestly, you can do A LOT with just these five, even if a different enumerable in niche situations could do the same task with less code.

Blocks

To ensure we’re on the same page, a piece of terminology: blocks. A block is what you want the enumerable to do with the information given to it. This could be mutating the content (x + 1), comparing size (x >1 or x>y), or just pushing it into the terminal.

array_of_elements = %w[dog cat mouse]
array_of_elements.each { |element| puts element }
# => dog
# => cat
# => mouse

Dog, cat, and mouse are each an element in an array. The block is within the curly braces and tells what should happen to each element, in this case printing it to the screen. The pipes (|element|) declare a variable, in this case the variable name is element. This variable is equal to first ‘dog’, then ‘cat’, and finally ‘mouse’, in other words each of them one at a time. We call the process of going from one element to the next iteration.

# Fun side note
example1 = %w[string string string] # percent string array
example2 = ['string', 'string', 'string']

You may have noticed the above syntax used in official documentation. In this case example1 and example2 are the same thing in the eyes of Ruby.

.each

The most basic of all of the methods we’ll be touching on today. As you might have inferred, .each iterates over everything given using the logic of the block. When given a block it then returns what it was originally given, regardless of the block. This is useful if you don’t want to keep any changes or just want to display what was given to .each.

.map

.map stands as one of the most common of all the enumerables. When given a block, it then returns the same number of elements mutated or transformed according to the logic of the block. If you need to change the same part(s) of every element you have, use .map. This could include capitalizing the first letter of an element in an array of strings, changing a number by a certain amount, etc.

.find/.find_all

Need to see if an array has a particular element? Then .find and .find_all will be your choice. For .find, when given a block, will return THE FIRST element which evaluates as true in the block. This will iterate through the given elements until the your block logic is true, then return that element. Typically this would mean comparing the element to something and if true it’s the chosen one, if not then it will continue looking. Technically, if the block is always true (1 == 1) then it will return the first element, even if it has nothing to do with 1 being the same as 1.

For .find_all, when given a block, will return EVERY element where the block evaluates as true. This is great when you want all of the same name, all even numbers, etc. However, .find_all ALWAYS returns an array. Even if it only discovers one truthy element.

So, if you need one of something every time, use .find. If you need more than one or could have more than one, use .find_all.

.reduce

When given a block, will return one from many. Understanding .reduce will help make .sum, .max, .min, and several other enumerables much easier to understand. This post is not intended to teach these enumberables, but I will give some quick highlights. Essentially, .reduce has the ability to store information that it can use during each step of the iteration. This allows you to compare the size of an element against all other elements only keeping the largest of each iteration, or to distill an array of numbers into their sum.

--

--

Logan McGuire

A creator to the core, he enjoys all games (especially collaborative ones), baking bread, and software development.