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.
-
What does
[1, 2, 3].eachwithout a block return?Answer
An Enumerator you drive by hand
Without a block, each returns an Enumerator, which is an external iterator.
-
What does this program print?
enum = [1, 2, 3].each puts enum.next puts enum.nextAnswer
1 / 2
Each next call returns the following element and moves the iterator on.
-
What happens when
nextis 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.
-
What does this program print?
result = (1..).lazy.map { |n| n * 2 }.first(5) p resultAnswer
[2, 4, 6, 8, 10]
The lazy chain computes only the five values that first asks for.
-
Why is
lazyneeded 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.
-
What does this program print?
counts = ["a", "b", "a"].each_with_object(Hash.new(0)) do |letter, memo| memo[letter] += 1 end p countsAnswer
{"a" => 2, "b" => 1}
The memo hash counts each letter, and a comes twice.
-
What does
each_with_objectpass to the block?Answer
The element and the memo object
The block mutates the memo, and the method returns it at the end.
-
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).inspectAnswer
[0, 1, 1, 2, 3, 5]
The loop yields a and steps the pair, and first stops after six values.
-
What does
y << ado insideEnumerator.new?Answer
Yields the value a to the consumer
The block parameter is the yielder, and << hands one value out.
-
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