Ruby and Enumerables

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.

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Confessions of an Intermediate Programmer

Hidden Shortcuts to Make Level Design Easier

Mac Control Volume Per App

Making web more inclusive with Manual Accessibility Testing — Galaxy Weblinks

Implementation of Spring Cloud Components.

Setting a starting player position — On the path to Unity development

5 Reason to choose MEAN Stack Development

Managing your first repository with Git — On the path to Unity development

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
Logan McGuire

Logan McGuire

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

More from Medium

Key Benefits Of Choosing Ruby On Rails For Your Project

Auto Save Action Text Data in Rails

MacBook Air on gray armchair

Rails still worth learning in 2022?

A ruby rookie’s tips and tricks for the newcomers | Part 1