Enumerator Objects
external iteration.
Enumerator Objects is a free Ruby Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
External Iteration
An Enumerator is an object that represents an iteration. It supports external iteration: you pull values one at a time with next, instead of handing a block to each.
enum = [10, 20, 30].each
puts enum.next
puts enum.nextGetting an Enumerator
Call an iterator method like each with no block and you receive an Enumerator instead of running.
enum = [1, 2, 3].each
puts enum.classPulling with next
next returns the following element each time you call it. The Enumerator remembers its position.
letters = ["a", "b", "c"].each
puts letters.next
puts letters.next
puts letters.nextStopIteration at the End
When values run out, next raises StopIteration. You can rescue it to stop cleanly.
enum = [1, 2].each
begin
loop { puts enum.next }
rescue StopIteration
puts "done"
endpeek and rewind
peek looks at the next value without advancing; rewind resets to the start.
enum = [5, 6, 7].each
puts enum.peek
puts enum.next
enum.rewind
puts enum.nextBuilding Custom Enumerators
Create your own with Enumerator.new. The block receives a yielder y; push values with y << or y.yield.
evens = Enumerator.new do |y|
n = 0
loop do
y << n
n += 2
end
end
puts evens.next
puts evens.next
puts evens.nextInfinite Enumerators
Because they are pull-based, custom Enumerators can be infinite. You take only what you need.
counter = Enumerator.new do |y|
i = 1
loop { y << i; i += 1 }
end
p counter.first(5)Enumerators Are Enumerable
An Enumerator includes Enumerable, so all the usual methods (map, select, take) work too, mixing internal and external iteration.
fib = Enumerator.new do |y|
a, b = 0, 1
loop do
y << a
a, b = b, a + b
end
end
p fib.take(8)with_index
with_index attaches an index to any Enumerator, optionally starting from a custom number.
["x", "y", "z"].each.with_index(1) do |item, i|
puts "#{i}: #{item}"
endReturning an Enumerator from each
In your own classes, return to_enum(:each) (or enum_for) when no block is given, so callers can iterate externally.
class Trio
def each
return to_enum(:each) unless block_given?
yield 1; yield 2; yield 3
end
end
e = Trio.new.each
puts e.next
puts e.nextPutting It Together
Enumerators give you fine-grained control:
- Pull values with
next, peek, or rewind. - Build custom ones with
Enumerator.newand a yielder. - They are Enumerable, so map/select/take all work.
odds = Enumerator.new do |y|
n = 1
loop { y << n; n += 2 }
end
p odds.first(4)Quick Check
Test your understanding of Enumerator objects.
Recap
You learned Enumerator objects:
- External iteration via
next,peek,rewind. StopIterationsignals the end.- Build custom ones with
Enumerator.new { |y| ... }. - They are Enumerable and can be infinite.
nums = Enumerator.new { |y| 1.upto(3) { |n| y << n } }
puts nums.next
puts nums.nextFrequently asked questions
Is the “Enumerator Objects” lesson free?
Yes — the full text of “Enumerator Objects” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Enumerator Objects”?
external iteration. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Enumerator Objects” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Writing Custom each
- Including Enumerable
- Lazy Enumerators
- Enumerator Objects