Project

General

Profile

Bug #15405 ยป endless-range.patch

zverok (Victor Shepelev), 12/12/2018 06:46 PM

View differences:

doc/syntax/literals.rdoc
(1..2) # includes its ending value
(1...2) # excludes its ending value
(1..) # endless range, representing infinite sequence from 1 to Infinity
You may create a range of any object. See the Range documentation for details
on the methods you need to implement.
range.c
* ('a'..'e').to_a #=> ["a", "b", "c", "d", "e"]
* ('a'...'e').to_a #=> ["a", "b", "c", "d"]
*
* == Endless Ranges
*
* Since Ruby 2.6, there is an "endless range" concept, representing
* half-infinite ranges. Literal for an endless range is:
*
* (1..)
*
* Which is equivalent to
*
* (1..nil)
* # or
* Range.new(1, nil)
*
* Endless ranges are useful, for example, for idiomatic slicing of
* arrays:
*
* [1, 2, 3, 4, 5][2..] # => [3, 4, 5]
*
* Some implementation details:
*
* * +end+ of endless range is +nil+;
* * +size+ of endless numeric range is +Infinity+;
* * +to_a+ of endless range raises +RangeError+;
* * +each+ of endless range enumerates infinite sequence (may be
* useful in combination with Enumerable#take_while or similar
* methods);
* * <code>(1..)</code> and <code>(1...)</code> are NOT equal,
* although technically representing the same sequence.
*
* == Custom Objects in Ranges
*
* Ranges can be constructed using any objects that can be compared
    (1-1/1)