1 minute read
In Ruby, a Range is an object that represents a range of values with a defined beginning and end. It’s a fundamental data structure used to express a sequence or span between two values, whether they are numeric, alphabetical, or even dates.
We have two ways to define ranges.
The Range#reverse_each method is used to iterate through a range of values in reverse order.
Using Range#reverse_each with very large or beginless or endless ranges never returns.
Under the hood Range#reverse_each uses Enumerable#reverse_each which is implemented with #to_a.
So we are virtually not able to use reverse_each for a very large or beginless or endless ranges, even if few elements are iterated on actually.
Ruby 3.3 adds Range#reverse_each for Integer elements, So we can use reverse_each for a very large or beginless ranges. Ref PR
With Ruby 3.3, Range#reverse_each for an endless range raises an exception, similar to Range#each for a beginless range. Ref PR