What is the difference between Lazy Enumerables and Eager evaluation?
Updated May 17, 2026
Short answer
Eager evaluation processes the whole collection at every step, creating intermediate arrays. Lazy evaluation applies transformations item-by-item, creating pipelines that support infinite collections.
Deep explanation
Standard Enumerable methods like map or select are eager; they evaluate the full collection and output a completed array before passing it to the next chained operation. For massive lists or infinite streams, this consumes huge memory and execution time. Prepending .lazy returns an instance of Enumerator::Lazy. This streams elements one at a time down the entire execution chain, allocating negligible intermediate memory.
Real-world example
Reading massive log files or database export archives line-by-line, filtering for anomalies without loading multi-gigabyte structures completely into application memory.
Common mistakes
- Using `.lazy` everywhere. For small collections, lazy processing adds substantial infrastructure wrap overhead, making execution slower than standard eager maps.
Follow-up questions
- How do you exit a lazy pipeline back into a real array?
- Can you use `#reduce` efficiently on an infinite lazy enumerable?