Exploring Enumerable Module in Ruby – A Complete Guide

ajith kumar

2 min read

Hi everyone, I’m excited to share my first blog post with you today! In this post, we’ll be exploring the Enumerable module in Ruby and its various methods.

The Enumerable module is a powerful tool that can be used to iterate over collections, filter elements, and perform other operations. We’ll be focusing on the (EEE) method, which is a bit confusing at first, but it’s actually quite powerful.

1. Enumeration:

It is a process of looping through the list of items one by one technically speaking it is termed as traversing or iterating. We will probably use a for loop to achieve the result, Wait there is a magic method already available that is #each. Where it came from you guessed right, Yeah that it belongs to the Enumerable module.


2. Enumerable:

In Ruby, we will name an object as enumerable when it represents a set of items and a method to iterate through each of its elements. It includes various methods such as #include? , #count , #map , #select and #uniq. Let’s get your hands a little bit dirty, Open your IRB or rails console try this out, and check how it works. Ex: [1,2,3].map { |n| n * 2 }

Note: Some methods, like #count and #take, are designed to work specifically with arrays, rather than using those provided by the Enumerable module.

3. Enumerator:

As a concept of simply instantiating by either creating an Enumerator. new or by invoking an instance method of an Enumerable object.
The Enumerable module works by using a method called #each, which should be available in any class that wants to use it. When you use #each with a block on an array, it performs the actions specified in the block for each item in the array. If you believe things are getting harder don’t worry you will understand better by running the below code on your machine.

If you call the #each method on an array without giving a block of code to execute for each of its elements, you will get an Enumerator object. In simple words, calling #each method without a block returns an Enumerator instance that lets you work with the array’s items in a versatile way, like iterating through them or applying other operations.

Enumerators allow you to loop through objects manually and chain multiple enumeration methods together. Simply, they give you control over how you traverse a collection of items, and you can perform various actions on those items in a sequence.

The #with_index method is a great example of how you can improve an enumerator’s functionality. In this case, you start by calling #each method on an array to get an enumerator. Then, by using #with_index, you can attach indices to the elements in the array, making it possible to print both the index and the element for each item in the collection.

Here are those common Enumerable module explained in a simpler way:

1. #each: To iterate each item in a collection one by one.

2. #map: Modify each element in the collection using a block of code and it returns the new updated collection.

3. #select: If you want to fetch items from the collection based on a condition.

4. #reduce: It’s useful when you want to combine all the elements in the collection into a single value by repeatedly applying an operation from the block.

5. #any?: It is used to check at least one element in the collection that matches the given condition.

Conclusion:

That’s a wrap. Thanks for reading! Hope I’ve encouraged you to understand and play around with one of the key concepts of the Enumerable module and its usage. As I said earlier, now you’re able to connect the (EEE), then you and I are just AWESOME.

These methods provide powerful ways to work with collections of data in Ruby.

To learn more about Enumerable here is the official doc: https://ruby-doc.org/3.2.2/Enumerable.html


Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *