Lazy Enumerators
Lazy evaluation.
Lazy Enumerators is a free Ruby Academy lesson on CoddyKit — lesson 3 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.
Lazy Evaluation
Normally Enumerable methods process the entire collection at once. A lazy enumerator processes elements one at a time, only as needed. This lets you work with huge or even infinite sequences.
result = (1..Float::INFINITY).lazy.map { |n| n * 2 }.first(5)
p resultThe Problem with Eager Methods
Calling map on an infinite range without lazy would never finish, because it tries to build a complete result array first.
# Eager version would hang. Lazy completes instantly:
first_three = (1..Float::INFINITY).lazy.select(&:even?).first(3)
p first_threeCreating a Lazy Enumerator
Call .lazy on any enumerable to get a lazy version. Chained methods then become lazy too.
lazy = (1..10).lazy
puts lazy.classNothing Runs Until Forced
Lazy chains build a recipe but do not execute. A terminal method like first, to_a, or force triggers the work.
chain = (1..Float::INFINITY).lazy.map { |n| n * n }
puts "Nothing computed yet"
p chain.first(4)Forcing Results
force (alias to_a) realizes a bounded lazy chain into an array.
result = (1..20).lazy.select(&:odd?).first(5)
p result
all = (1..6).lazy.map { |n| n * 10 }.force
p allChaining Lazily
Multiple lazy operations are interleaved per element rather than run as separate full passes. Each value flows through the whole chain before the next begins.
result = (1..Float::INFINITY)
.lazy
.map { |n| n * n }
.select { |n| n.even? }
.first(4)
p resulttake and take_while
Lazy take and take_while stop pulling values once their condition is met.
result = (1..Float::INFINITY).lazy.take_while { |n| n < 6 }.to_a
p resultInfinite Sequences
Lazy enumerators shine with generators. Pair them with Enumerator to model endless streams.
naturals = Enumerator.new do |y|
n = 1
loop { y << n; n += 1 }
end
p naturals.lazy.map { |x| x * 3 }.first(5)Memory Efficiency
Because lazy chains never materialize the full intermediate collection, they save memory when filtering large data down to a few results.
big = (1..1_000_000).lazy
first_match = big.select { |n| n % 7 == 0 && n % 11 == 0 }.first
puts first_matchWhen to Use Lazy
Use lazy when:
- The source is infinite or very large.
- You only need a few results.
- You want to avoid big intermediate arrays.
For small collections, eager methods are simpler and fast enough.
fizz = (1..Float::INFINITY).lazy.select { |n| n % 3 == 0 }.first(5)
p fizzPutting It Together
Lazy enumerators defer work:
.lazymakes a chain lazy.- Work runs only when a terminal method pulls values.
- Perfect for infinite or huge sequences.
squares = (1..Float::INFINITY).lazy.map { |n| n * n }
p squares.select(&:odd?).first(4)Quick Check
Test your understanding of lazy enumerators.
Recap
You learned lazy enumerators:
.lazydefers computation.- Terminal methods (
first,to_a,force) trigger work. - Each value flows through the whole chain on demand.
- Ideal for infinite or large sequences.
p (1..Float::INFINITY).lazy.select(&:even?).map { |n| n + 1 }.first(3)Frequently asked questions
Is the “Lazy Enumerators” lesson free?
Yes — the full text of “Lazy Enumerators” 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 “Lazy Enumerators”?
Lazy evaluation. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Lazy Enumerators” 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