Table of contents
1.
Introduction
2.
Enumerable Objects in Ruby
3.
Examples
3.1.
Example 1
3.2.
Example 2
4.
Different operations on enumerable objects in Ruby
5.
Frequently Asked Questions
5.1.
What is Collection?
5.2.
What is Enumerable?
5.3.
What are Enumerable objects?
5.4.
What is the collect enumeration iterator?
5.5.
What is the select enumeration iterator?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Enumerable Objects in Ruby

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Before preceding the concept of enumerable objects in ruby, let's discuss what a collection is?

A collection is any class that represents a collection of values. Array and Hash are the key collection classes in Ruby, and the standard library adds a Set class. Each collection class mixes in the Enumerable module, which means that Enumerable methods are universal collection methods. 

Therefore, The Enumerable module is a mixin that implements several practical methods on top of the "each" iterator.

So, let's discuss the enumerable objects in ruby.

Enumerable Objects in Ruby

Enumerable Objects in Ruby

An object is enumerable when describing a set of items and the method to loop over each.

In Ruby, inheritance is limited to a single parent class, but enumerable allows us to share functionality between unrelated classes.

The enumerable module provides us with methods for searching, traversal, and sorting collections.

The benefit of enumerability is that we don't have to rewrite the function. We only have to mix in the enumerable objects to deal with collections.

Now the question that comes to mind is how we can make our class enumerable?

To use an enumerable module, we include it in our class and provide "each" iterator that passes each element of the collection to the associated block. 


Let's see some examples of enumerable objects in ruby to understand it better.

Examples

Let us see some examples to understand enumerable objects in Ruby.

Example 1

In this example, we don't have to write the ‘to_a’ method ourselves as we can get it free by including the ‘Enumerable’ module.

class CN
attr_accessor :name
include Enumerable
def initialize(name)
@name = name
end
def each
yield name
end
end

CN.new("Hello").to_a 
#-> Hello
You can also try this code with Online Ruby Compiler
Run Code

 

  • attr_accessor is used when you need both attr_reader and attr_writer.
  • attr_reader -> Method to read data.
  • attr_writer -> Method to write data.

Example 2

In this example, we don’t have to write the sort function because by including the “Enumerable’ module we can get it free.

class Person
attr_accessor :emails
include Enumerable
end

me = Person.new
me.emails = ["college@gmail.com", "work@gmail.com", "blog@codingninjas.com"]
me.emails.sort
#-> blog@codingninjas.com, college@gmail.com, work@gmail.com
You can also try this code with Online Ruby Compiler
Run Code

Different operations on enumerable objects in Ruby

Let us have a look at the different operations on enumerable objects in Ruby

Finding minimum and maximum value.

We can find the minimum and maximum using the Enumerable.
[1,2,3,4,5].min
#-> 1
[1,2,3,4,5].max
#-> 5
You can also try this code with Online Ruby Compiler
Run Code

 

Reset an enumerator.

We can also rewind the number.

x=[1,2,3,4,5].map
x.next #->1
x.next #->2
x.next #->3
x.rewind #->1
You can also try this code with Online Ruby Compiler
Run Code

 

Removing duplicates.

We can also remove the duplicate items from the array using Enumerable.

["Ram," "Shyam," "Ganesh," "Ram," "Mahesh," "Ganesh"].uniq
#-> Ram, Shyam, Ganesh, Mahesh
You can also try this code with Online Ruby Compiler
Run Code

 

Calculation of size or length.

We can find the length using Enumerable.

[“Ninjas”, 1, Ranchi, false].length
#->  4
You can also try this code with Online Ruby Compiler
Run Code


We had seen the concept of enumerable objects in ruby with examples. Now let's see some FAQs related to them.

Frequently Asked Questions

What is Collection?

A collection is any class that represents a collection of values. Example- Array, Hash.

What is Enumerable?

An enumerable module allows us to share functionality between unrelated classes.

What are Enumerable objects?

The Enumerable module is a mixin that implements many proper methods on top of the "each" iterator.

What is the collect enumeration iterator?

The collect method executes its associated block for each element of the enumerable object and collects the return values of the blocks into an array.

What is the select enumeration iterator?

The select method invokes the associated block for each element in the enumerable object. It returns an array of elements for which the block returns a value other than false or nil.

Conclusion

In this article, we discussed enumerable objects in ruby with examples. We also discussed different operations performed on the enumerable objects in ruby.

After reading about the enumerable objects in ruby, are you not feeling excited to read/explore more articles on various CMS Platforms? Don't worry; Coding Ninjas has you covered. See RubyIntroduction in RubyExpressions, Operators in RubyMethods in Ruby, and Learn Ruby. You can also refer to the Official RubyRubyOfficial DocumentationRuby FAQ, and Ruby Koans.

Check out this problem - Find Duplicate In Array

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Conclusion Image

Live masterclass