[前][次][番号順一覧][スレッド一覧]

ruby-changes:54148

From: marcandre <ko1@a...>
Date: Thu, 13 Dec 2018 04:49:28 +0900 (JST)
Subject: [ruby-changes:54148] marcandRe: r66369 (trunk): range.c: Documentation on endless ranges.

marcandre	2018-12-13 04:49:22 +0900 (Thu, 13 Dec 2018)

  New Revision: 66369

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66369

  Log:
    range.c: Documentation on endless ranges.
    
    Based on patch by Victor Shepelev [DOC] [#7552]

  Modified files:
    trunk/doc/syntax/literals.rdoc
    trunk/range.c
Index: range.c
===================================================================
--- range.c	(revision 66368)
+++ range.c	(revision 66369)
@@ -1485,6 +1485,34 @@ range_alloc(VALUE klass) https://github.com/ruby/ruby/blob/trunk/range.c#L1485
  *     ('a'..'e').to_a    #=> ["a", "b", "c", "d", "e"]
  *     ('a'...'e').to_a   #=> ["a", "b", "c", "d"]
  *
+ *  == Endless Ranges
+ *
+ *  An "endless range" represents and semi-infinite ranges.
+ *  Literal notation for an endless range is:
+ *
+ *     (1..)
+ *     # or similarly
+ *     (1...)
+ *
+ *  Which is equivalent to
+ *
+ *     (1..nil)  # or similarly (1...nil)
+ *     Range.new(1, nil) # or Range.new(1, nil, true)
+ *
+ *  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+;
+ *  * +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
Index: doc/syntax/literals.rdoc
===================================================================
--- doc/syntax/literals.rdoc	(revision 66368)
+++ doc/syntax/literals.rdoc	(revision 66369)
@@ -333,6 +333,7 @@ its ending value. https://github.com/ruby/ruby/blob/trunk/doc/syntax/literals.rdoc#L333
 
   (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.

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]