Enumerables: A (Complete) Beginner’s Guide
As someone completely new to programming, enumerables have been one of the most difficult concepts for me to grasp. After several YouTube videos, reading the Ruby docs (https://ruby-doc.org/core-2.6.5/Enumerable.html) and plenty of practice I am finally starting to feel comfortable using them. I hope this guide helps you get your enumerable methods up and running in no time! So, what is an enumerable anyway?
The Basics
Simply put, enumerables are methods that allow us to look through an array or hash (I’ll be focusing on arrays for simplicity here). The simplest method, .each, iterates over each item in the array but does not alter the array. Depending on which method we use an enumerable can transform, sort, add or perform one of many other functions found in the enumerable docs.
The biggest challenge I had when learning how to use an enumerables was learning the syntax and what it means. Here is a basic format of that syntax:
array.enumerable_method{|thing| thing.method}
First, we need to select the array we want to iterate through, which is represented by ‘array’ above.
Second, we want to perform an action on that array (.enumerable_method). For example, if we wanted know the sum of an array of integers, the .sum method would be a lot easier than manually going through and adding up each value in the array.
Third, some methods will use a block ({} or do…end) to specify which action we want to perform on each element in the array. |thing| is what we will be iterating over. If the array was comprised of the names of students, student or name would be an appropriate/easy way of keeping track of our goal. |thing| represents a single element in the array, so |student| would represent one student’s name.
Finally, we might want to provide some further detail about what action we want to perform on our array by passing a method in our block. If this is confusing, the following examples should help clear it up.
.each
As I mentioned above, .each is the simplest Ruby enumerable and it simply goes through the array. For these examples I will use the following array of National Parks:
nps = [“Zion”, “Glacier”, “Shenandoah”, “Great Smoky Mountains”, “Bryce Canyon”, “Grand Teton”, “Hot Springs”, “Joshua Tree”, “Grand Canyon”, “Rocky Mountain”]
One application of each is to fill in forms with unique values. We know our Parks are beautiful, but I’m lazy and don’t want to type (Park) is beautiful 10 times. .each can make this much easier!
nps.each{|park| puts "#{park} is beautiful!"}
Copy and paste the array and code above into a console and you should see the following in your terminal:

And with that one line of code I was able to puts out “(Park) is beautiful!” ten times. Something important to note is that .each will retain and return the original array. Now that we’ve got the basics, let’s check out some other methods.
.map
.map is similar to .each, but allows us to alter the original array. In the following example, I used the .downcase method to make all of the Parks lower case.
nps.map{|park| park.downcase}
Will return:

.map will go to each and every index in the array and perform the action specified in the block, returning an array with the same length, but not the same values.
.select
.select can be used to filter out certain values from an array. In this case, I only want to see Parks who’s names are eleven characters long.
nps.select{|park| park.length == 11}
Will return:

.select iterates through the nps array but only returns values that meet the condition .length == 11. .length could be swapped out for any method that would work on the data type in the array (in this example a string).
.find
.find is VERY similar to .select, except it will only return the first value to meet the given condition.
nps.find{|park| park.length == 11}
Will return:

Notice that .select returns an array because there are multiple matches for park.length == 11. With .find we only care about the first value to meet our condition, so it will only return that value, in this instance, a string.
.count
.count will return an integer based on the condition passed through. I’m curious about how many Parks in my array start with the letter “g”.
nps.count{|park| park.start_with? “G”}
Will return:

In my array nps, there are four Parks that start with “g”. If I were to add another, like Glacier Bay and rerun the method, the count would update to 5. Like .map, .find and .select, the condition is extremely flexible.
.sort and .sort_by
Two other similar methods are .sort and .sort_by. .sort can be used with a block but so far I haven’t used it too much. .sort without a block will return the array in alphabetical order if the values are strings, or from smallest to largest if they are integers. If we want to have our array sorted Z-A or with the largest number first, just add .reverse.
nps.sort.reverse
Will return:

The variant of .sort that I find myself using more often so far is .sort_by. This allows us to pass in a condition, such as length.
nps.sort_by{|park| park.length}
Will return:

Just like .sort, .sort_by will sort with the smallest value first and adding . reverse at the end of the method will sort with the largest first. Adding in a condition makes our iteration more powerful and organizing data much, much easier!
I hope you guys enjoyed this guide and that it helps make learning what an enumerable is and does easier! I barely scratched the surface of what you can do with enumerables, so try playing around with my array and some of the methods above or make some of your own. When you’re comfortable with the basics check out the (probably) 50 others that I didn’t cover!