Understanding Lazy Enumerables

Let’s do a brief recap on terminology before we dive into lazy enumerables. What’s the difference between an enumerable and an enumerator?

In Ruby, an Enumerable is a collection class (such as Array and Hash) that contains methods for traversal, searching, and sorting.

An Enumerator, on the other hand, is an object that performs the actual enumeration. There are two kinds of enumeration—internal and external—which will be explained in External vs. Internal Iteration.

Lazy enumeration was introduced in Ruby 2.0. What exactly is lazy? Is Ruby trying to slack off? Well, the “lazy” in lazy enumeration refers to the style of evaluation. To understand this better, let’s review the opposite of lazy evaluation: ...

Get Mastering Ruby Closures now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.