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 Ruby, Introduction in Ruby, Expressions, Operators in Ruby, Methods in Ruby, and Learn Ruby. You can also refer to the Official Ruby, Ruby, Official Documentation, Ruby 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!
