Skip to content

Range#each: iterate an endless range lazily instead of materialising it - #457

Merged
tannevaled merged 1 commit into
mainfrom
feat/cluster27
Aug 25, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/cluster27

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Range#each built the whole element list with rangeElems, which raised
can't iterate from <begin> for an endless range (1.., "a"..) because such a
range has no finite element list. It now iterates an endless range lazily,
the way Range#step already does: a String begin walks by #succ, an Integer or
Bignum begin counts up with exact big.Int arithmetic (so an endless Bignum
range does not degrade to Float and stall), and the block breaks out of the
otherwise infinite walk. A begin with no successor (e.g. a Float) still raises a
TypeError.

Because every Enumerable method routes through #each, this also makes
each_with_index, select, map, lazy, first(n), etc. work on an endless
range. Bounded ranges keep their existing materialising path.

Verified against ruby 4.0.6. Full package coverage stays at 100.0%.
Zero-regression rubyspec sweep; core/range/each_spec 5 → 7. (It also
unblocks the endless-source example of Enumerator.product once that lands.)

🤖 Generated with Claude Code

Range#each built the whole element list with rangeElems, which raised
"can't iterate from <begin>" for an endless range (1.., "a"..) because such a
range has no finite element list. It now iterates an endless range lazily, the
way Range#step already does: a String begin walks by #succ, an Integer or Bignum
begin counts up with exact big.Int arithmetic (so an endless Bignum range does
not degrade to Float and stall), and the block breaks out of the otherwise
infinite walk. A begin with no successor (e.g. a Float) still raises a TypeError.

Because every Enumerable method routes through #each, this also makes
each_with_index, select, map, lazy, first(n), etc. work on an endless range.
Bounded ranges keep their existing materialising path.

Verified against ruby 4.0.6. Full package coverage stays at 100.0%. Zero-
regression rubyspec sweep; core/range/each_spec 5 -> 7. (It also unblocks the
endless-source example of Enumerator.product once that lands.)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 6fa3bf3 into main Aug 25, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/cluster27 branch August 25, 2026 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant