Enumerators are objects that yield things to a code block.
y is a “yielder,” an instance of
Enumerator::Yielder. The yielder is populated in the code block passed to the enumerator, and the enumerator looks to the yielder to determine what to return at any particular iteration of an
You can create an enumerator from most iterator methods by withholding a code block.
The enumerator retains whatever method it’s created from — here
e.each is created from
a.map and functions like
You can even use an enumerator to do work on infinite sets by making it a “lazy” enumerator. Here I wrote a function to find primes in the first
n positive natural numbers.