Take a break!
The Enumerable
module defines the lazy
method that will transform your iterators into streams. In addition to being able to handle infinite Ranges
more easily, it will be able to perform more efficiently.
It works by changing the order of execution. Instead of proceeding « by method « , it proceeds « by element « , avoiding unnecessary operations whenever possible.
The eager
method transforms back into a classic iterator (without losing the advantages defined so far) to be transparent if necessary.
Link to Ruby documentation for lazy
.