Ruby: Enumerable Module
A module that we have used many times before is the Enumerable
module. It is a collection of iterative methods, and is included in the Hash
and Array
classes. Enumerable methods give us a more concise way of iterating over elements in a collection than a for
loop, and also provide us with many useful operations.
Let’s have a look at a few of these enumerable methods.
Each and Map
#each
is a method that iteratates over each element in an array, running the block for every element and returns the original elements that it was called upon. For example:
In order to get a return of the abc
string array as changed by the expression within the block, a new empty array would have to be declared, the changed elements shovelled into it, and the new array returned OR #map
could be used. #map
sets the current element in the iteration to the return value of the block, and returns the changed element. For example:
Here you can see how the method “maps” each element to a new state.
Select and Reject
Select iterates over a list of elements and returns a new list where the elements fit a certain criterium, or certain criteria, that it was passed. For instance, if we want to find and return numbers in an array that are divisible by three, we could use #select
like so:
When we want to do the opposite, and return elements that don’t fit the criterium of being divisible by three, we can instead use #reject
. For example:
Inject
#inject
iterates over elements and runs a block where element a
interacts with the next element b
, the result of which interacts in the same way with element c
. This is particularly useful when you want to sum elements within an array, like so:
What inject is doing here is this:
Of course, another way to look at what #inject
essentially did, was that it put a +
in between every element and then returned the result. It injects every element with addition. So naturally, Ruby has a nice way to call #inject
more concisely, like so:
Enumerators
An Enumerator
is another Ruby class that uses the Enumerable
module. Enumerable
is the collection of methods that iterate over elements, it is the collection of iterative processes. Therefore an Enumerator
can be thought of as an object form of Enumerable
, a tool that performs iteration. Enumerator
is a class, and can therefore be instantiated. If you use an Enumerable
method on an Array or Hash without passing it a block, an instance of Enumerator
will be created. Then you can cycle through the Array or Hash using the Enumerator
method #next
, like so:
If you would like more explanation about this, Avdi Grimm’s Ruby Tapas episode for Enumerator provides an excellent video tutorial.