Enumerators

External iteration

Calling each without a block returns an Enumerator, which is an iterator you drive by hand.

Each next call returns the following element, which helps when input arrives in steps.

enum = [1, 2, 3].each puts enum.next puts enum.next

ruby enum_demo.rb

1
2

Lazy chains

Long or endless sequences need lazy chains, so values are computed only on demand.

The lazy call turns the endless range into a lazy enumerator, and first pulls just five doubled values.

result = (1..).lazy.map { |n| n * 2 }.first(5) p result

ruby lazy_demo.rb

[2, 4, 6, 8, 10]

each_with_object

The each_with_object method folds a collection into a memo object that the block mutates.

A hash with default zero counts letters without any key checks.

counts = ["a", "b", "a"].each_with_object(Hash.new(0)) do |letter, memo| memo[letter] += 1 end p counts

ruby counts_demo.rb

{"a" => 2, "b" => 1}

A Fibonacci enumerator

Custom sequences come from Enumerator.new with a block that yields each value.

An endless loop is safe here because first stops the sequence after six numbers.

fib = Enumerator.new do |y| a = 0 b = 1 loop do y << a a, b = b, a + b end end puts fib.first(6).inspect

ruby fib_demo.rb

[0, 1, 1, 2, 3, 5]

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does [1, 2, 3].each without a block return?

    Answer

    An Enumerator you drive by hand

    Without a block, each returns an Enumerator, which is an external iterator.

  2. What does this program print? enum = [1, 2, 3].each puts enum.next puts enum.next

    Answer

    1 / 2

    Each next call returns the following element and moves the iterator on.

  3. What happens when next is called on an enumerator that has run out?

    Answer

    It raises StopIteration

    The enumeration ends with StopIteration, which a loop catches and a raw call sees.

  4. What does this program print? result = (1..).lazy.map { |n| n * 2 }.first(5) p result

    Answer

    [2, 4, 6, 8, 10]

    The lazy chain computes only the five values that first asks for.

  5. Why is lazy needed for an endless range?

    Answer

    It computes values on demand instead of all at once

    Without lazy, the chain would try to walk the whole endless range first.

  6. What does this program print? counts = ["a", "b", "a"].each_with_object(Hash.new(0)) do |letter, memo| memo[letter] += 1 end p counts

    Answer

    {"a" => 2, "b" => 1}

    The memo hash counts each letter, and a comes twice.

  7. What does each_with_object pass to the block?

    Answer

    The element and the memo object

    The block mutates the memo, and the method returns it at the end.

  8. What does this program print? fib = Enumerator.new do |y| a = 0 b = 1 loop do y << a a, b = b, a + b end end puts fib.first(6).inspect

    Answer

    [0, 1, 1, 2, 3, 5]

    The loop yields a and steps the pair, and first stops after six values.

  9. What does y << a do inside Enumerator.new?

    Answer

    Yields the value a to the consumer

    The block parameter is the yielder, and << hands one value out.

  10. What does first(3) do to an endless enumerator?

    Answer

    Takes three values and stops the sequence

    first asks for that many values and stops pulling, which is why an endless loop is safe.

Article author: Arthur Isaev