Common Enumerables in Ruby

Sam Hall
4 min readJan 7, 2021

Enumerable refers a method provided by Ruby that accesses, or iterates over, every element in a collection of data and can manipulate said element in whichever way you choose!

Yeah, I thought coding would be like this too. Alas, we have to learn the very basics first.

.all?

By using .all? on an array, every element accessed will be tested against rules set by the block, and will only return true if all of the elements meet the block’s requirements:

For example, if all of the values in a given array are greater than 1, our return value is true.

But if some of the values don’t meet the block’s requirement…

Here, we’ve set the block parameter to greater than or equal to 50, and because not all of the elements in the array are greater than 50, our iteration method returns false.

.any?

Conversely, using .any? on an array of elements will test if any of said elements meet the requirement of the block, and if they do, will return a value of true:

And if any of the elements do not meet the blocks requirement…

We get a return value of false. Because we’ve set the block to test whether or not any of the values are greater than or equal to the highest value in the array, the return value is false.

.collect or .map

Using .collect (which is interchangeable with .map) will access each element in an array and use the block to make changes to each element:

When we take an array of integers, and use .collect to change each integer into a string, our return value will yield and array of strings.

And just to prove that .map is totally interchangeable, here’s proof!

Ha! The exact same return value of an array of strings. Told ya!

.count

When we call .count. on an array of element, we intend to count the number of elements in said array, and return an integer value:

Because we had six elements in the array, using the .collect enumerable returned an integer value of 6.

.find

Using the .find method will iterate through an array of elements and provide the first element in the expression that returns true based on the block provided.

For instance, if we call .find on this array, our return value is the first element that satisfies the block provided (“greater than 6”), which is the integer element 50.

.find_all or .select

Using .find_all or .select on and array of elements will find all elements that satisfy the block provided. So if our block specifies that we want to return any element whose value is an even integer…

We retrieve and return an array of the elements that are even in the given array.

And if you weren’t convinced, .select works in the same way:

Both operate by iterating through elements and returning those elements that satisfy our block. Use .select when you don’t feel like typing that extra underscore.

.max

We can use .max whenever we want to find the highest value in an array of elements:

As expected, our return value is the highest value in our array of integers.

.min

Conversely, .min returns the lowest value in the array of elements:

Here, our return value is 5: the lowest value element.

Welp, this is my first blog post and hopefully not my last. I hope it was helpful.

--

--