Since Ruby 2.0, Enumerator::Lazy
has existed. It'll let you
enumerate over a list of values, like this:
That's not so spectacular, though, and iterating over 10,000 values isn't
particularly unusual. The #lazy
method becomes really useful when
we're dealing with huge enumerations:
That first line never finishes, because #map
can never finish
iterating over the infinite range. So, if you're looking to work with a
collection, but you don't need to access the whole thing at once, then
#lazy
is a good way forward.
Parsing huge files
is a nice example.