The Magic of Ruby’s Enumerable Methods

Throughout my coding journey, there were many times where I just sat and stared at my screen to figure out what someone else had written. Whenever I had to dissect code to understand what was happening, I found that the more lines of code there were, the easier it was to break down. I was able to clearly pinpoint and visualize exactly what was happening line by line.

The curve ball arrived when I learned that writing one small word could be as powerful as writing 15 lines of code. It baffled me that it could be that simple. How could one word be able to accomplish so many things? I thought coding was supposed to be tricky and deceptive. I had the perceived notion that everything had to look like a wall of hieroglyphics.

Let’s say you want to find a specific number within an array. You could write out a while loop method that could do just that.

numbers = [1, 2, 3, 4, 5]def find_element(array, value_to_find)
count = 0
while count < array.length do
if array [count] == value_to_find
return value_to_find
else nil
count += 1
find_element(numbers, 2)

=> 2

Now that’s a lot to digest. Sure, you could read this line by line and slowly decipher what exactly is happening. But, did you know the same example above can be written like this?

[1, 2, 3, 4, 5].find {|i| i == 2}=> 2

Much better! It’s easy and clean to read. That’s the magic of an enumerable. So what exactly is an enumerable?

An enumerable is a method provided by Ruby that allows us to visit every element or pair in a collection (like looping), and then perform an action on that element. These actions can be ones like calculating, modifying, comparing, singling out, reducing, adding, etc... The possibilities are endless. If you need an action performed on a set of data, there’s most likely an enumerable out there that can get the job done for you. This saves you so much time rather than writing out loops. Pretty sweet! This link will give you an idea of what each enumerable can do.

Even with all these enumerable’s, Ruby makes it simpler by allowing us to use the .each method to solve many of our problems.

Photo by Muukii on Unsplash

But this is bad practice. We want to be as descriptive as possible when we enumerate over data so that it’s very clear as to what we are trying to produce with our data. If you find yourself in the habit of using the each enumerable, you should try to use other options. There is a good chance that a more descriptive enumerable exists to take on the given task.

Let’s look at this example…

[1, 2, 3, 4, 5].select do|number|
if number.even?
puts number
=> 2

Here we were able to find out which numbers in the array were even values. the .select enumerable looped through each value in the array and selected the values we wanted given the condition of “number.even?”. It goes hand in hand that if you wanted to select certain values, you would use the .select enumerable.

We could also get the same result using .each but that wouldn’t give us much information on what we are trying to accomplish.

The great thing about Ruby is that it is such a readable and descriptive language. You as the speaker and writer of this language should be as descriptive as possible since you are afforded that luxury given the syntax of Ruby.

With this knowledge of enumerable’s, you now have a very powerful tool in your arsenal. You will be able to follow the less is more motto while still being descriptive as to what you are trying to accomplish. Let your imagination run wild now that you’ve uncovered the magic of enumerable’s. Happy coding!

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