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.
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
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.
The same way, we can use eager method when we pass lazy enumerator as an argument to
any method which expects a normal enumerator.