concept lazy enumerator in category ruby

appears as: Lazy enumerators, lazy enumerator
The Well-Grounded Rubyist, Third Edition

This is an excerpt from Manning's book The Well-Grounded Rubyist, Third Edition.

You can get a finite result from an infinite collection by using a lazy enumerator. Calling the lazy method directly on a range object will produce a lazy enumerator over that range:

>> (1..Float::INFINITY).lazy
=> #<Enumerator::Lazy: 1..Infinity>

You can then wire this lazy enumerator up to select, creating a cascade of lazy enumerators:

Lazy enumerators are a somewhat specialized tool, and you probably won’t need them too often. But they’re very handy if you have an infinite collection and want to deal only with a finite result set from operations on that collection.

You can use a lazy enumerator to write a version of FizzBuzz that can handle any range of numbers. Here’s what it might look like: