Ruby 2.7 adds Enumerator::Lazy#eager

Ashik Salman

By Ashik Salman

on March 18, 2020

This blog is part of our  Ruby 2.7 series.

Ruby 2.0 introduced Enumerator::Lazy, a special type of enumerator which helps us in processing chains of operations on a collection without actually executing it instantly.

By applying Enumerable#lazy method on any enumerable object, we can convert that object into Enumerator::Lazy object. The chains of actions on this lazy enumerator will be evaluated only when it is needed. It helps us in processing operations on large collections, files and infinite sequences seamlessly.

1# This line of code will hang and you will have to quit the console by Ctrl+C.
2irb> list = (1..Float::INFINITY).select { |i| i%3 == 0 }.reject(&:even?)
4# Just adding `lazy`, the above line of code now executes properly
5# and returns result without going to infinite loop. Here the chains of
6# operations are performed as and when it is needed.
7irb> lazy_list = (1..Float::INFINITY) { |i| i%3 == 0 }.reject(&:even?)
8=> #<Enumerator::Lazy: ...>
10irb> lazy_list.first(5)
11=> [3, 9, 15, 21, 27]

When we chain more operations on Enumerable#lazy object, it again returns lazy object without executing it. So, when we pass lazy objects to any method which expects a normal enumerable object as an argument, we have to force evaluation on lazy object by calling to_a method or it's alias force.

2# Define a lazy enumerator object.
3irb> list = (1..30) { |i| i%3 == 0 }.reject(&:even?)
4=> #<Enumerator::Lazy: #<Enumerator::Lazy: ... 1..30>:select>:reject>
6# The chains of operations will return again a lazy enumerator.
7irb> result = { |x| x if x <= 15 }
8=> #<Enumerator::Lazy: #<Enumerator::Lazy: ... 1..30>:select>:reject>:map>
10# It returns error when we call usual array methods on result.
11irb> result.sample
12irb> NoMethodError (undefined method `sample'
13irb> for #<Enumerator::Lazy:0x00007faab182a5d8>)
15irb> result.length
16irb> NoMethodError (undefined method `length'
17irb> for #<Enumerator::Lazy:0x00007faab182a5d8>)
19# We can call the normal array methods on lazy object after forcing
20# its actual execution with methods as mentioned above.
21irb> result.force.sample
22=> 9
24irb> result.to_a.length
25=> 3

The Enumerable#eager method returns a normal enumerator from a lazy enumerator, so that lazy enumerator object can be passed to any methods which expects a normal enumerable object as an argument. Also, we can call other usual array methods on the collection to get desired results.

2# By adding eager on lazy object, the chains of operations would return
3# actual result here. If lazy object is passed to any method, the
4# processed result will be received as an argument.
5irb> eager_list = (1..30) { |i| i%3 == 0 }.reject(&:even?).eager
6=> #<Enumerator: #<Enumerator::Lazy: ... 1..30>:select>:reject>:each>
8irb> result = { |x| x if x <= 15 }
9irb> result.sample
10=> 9
12irb> result.length
13=> 3

The same way, we can use eager method when we pass lazy enumerator as an argument to any method which expects a normal enumerator.

1irb> list = (1..10) { |i| i%3 == 0 }.reject(&:even?)
2irb> def display(enum)
3irb> { |x| p x }
4irb> end
6irb>  display(list)
7=> #<Enumerator::Lazy: #<Enumerator::Lazy: ... 1..30>:select>:reject>:map>
9irb> eager_list = (1..10) { |i| i%3 == 0 }.reject(&:even?).eager
10irb> display(eager_list)
11=> 3
12=> 9

Here's the relevant commit and feature discussion for this change.

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, ReactJS, React Native, remote work,open source, engineering & design.