Project

General

Profile

Bug #15405 ยป endless-range.patch

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

View differences:

doc/syntax/literals.rdoc
333 333

  
334 334
  (1..2)  # includes its ending value
335 335
  (1...2) # excludes its ending value
336
  (1..)   # endless range, representing infinite sequence from 1 to Infinity
336 337

  
337 338
You may create a range of any object.  See the Range documentation for details
338 339
on the methods you need to implement.
range.c
1485 1485
 *     ('a'..'e').to_a    #=> ["a", "b", "c", "d", "e"]
1486 1486
 *     ('a'...'e').to_a   #=> ["a", "b", "c", "d"]
1487 1487
 *
1488
 *  == Endless Ranges
1489
 *
1490
 *  Since Ruby 2.6, there is an "endless range" concept, representing
1491
 *  half-infinite ranges. Literal for an endless range is:
1492
 *
1493
 *     (1..)
1494
 *
1495
 *  Which is equivalent to
1496
 *
1497
 *     (1..nil)
1498
 *     # or
1499
 *     Range.new(1, nil)
1500
 *
1501
 *  Endless ranges are useful, for example, for idiomatic slicing of
1502
 *  arrays:
1503
 *
1504
 *    [1, 2, 3, 4, 5][2..]   # => [3, 4, 5]
1505
 *
1506
 *  Some implementation details:
1507
 *
1508
 *  * +end+ of endless range is +nil+;
1509
 *  * +size+ of endless numeric range is +Infinity+;
1510
 *  * +to_a+ of endless range raises +RangeError+;
1511
 *  * +each+ of endless range enumerates infinite sequence (may be
1512
 *    useful in combination with Enumerable#take_while or similar
1513
 *    methods);
1514
 *  * <code>(1..)</code> and <code>(1...)</code> are NOT equal,
1515
 *    although technically representing the same sequence.
1516
 *
1488 1517
 *  == Custom Objects in Ranges
1489 1518
 *
1490 1519
 *  Ranges can be constructed using any objects that can be compared