I'm interested in how values are fetched from an Enumerator object. In the following piece of code, I was expecting the first enum.next call to raise an exception as all values had already been received from enum after the call to enum.to_a.
enum = Enumerator.new do |yielder|
yielder.yield 1
yielder.yield 2
yielder.yield 3
end
p enum.to_a # => [1, 2, 3]
puts enum.next # Expected StopIteration here
puts enum.next
puts enum.next
puts enum.next # => StopIteration exception raised
What's the difference between calling next vs an iterator method like to_a on an instance of Enumerator?