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

ruby-changes:35782

From: akr <ko1@a...>
Date: Fri, 10 Oct 2014 18:43:01 +0900 (JST)
Subject: [ruby-changes:35782] akr:r47864 (trunk): Update doc. and warnings.

akr	2014-10-10 18:42:47 +0900 (Fri, 10 Oct 2014)

  New Revision: 47864

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

  Log:
    Update doc. and warnings.

  Modified files:
    trunk/enum.c
Index: enum.c
===================================================================
--- enum.c	(revision 47863)
+++ enum.c	(revision 47864)
@@ -2835,12 +2835,6 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yield https://github.com/ruby/ruby/blob/trunk/enum.c#L2835
  *      }
  *    }
  *
- *  If the block needs to maintain state over multiple elements,
- *  an +initial_state+ argument can be used.
- *  If a non-nil value is given,
- *  a reference to it is passed as the 2nd argument of the block for the
- *  +chunk+ method, so state-changes to it persist across block calls.
- *
  */
 static VALUE
 enum_chunk(int argc, VALUE *argv, VALUE enumerable)
@@ -2853,7 +2847,7 @@ enum_chunk(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L2847
 	rb_raise(rb_eArgError, "no block given");
     n = rb_scan_args(argc, argv, "01", &initial_state);
     if (n != 0)
-        rb_warn("initial_state given for chunk.  (Use Enumerator.new with lexical scope variables.)");
+        rb_warn("initial_state given for chunk.  (Use local variables.)");
 
     enumerator = rb_obj_alloc(rb_cEnumerator);
     rb_ivar_set(enumerator, rb_intern("chunk_enumerable"), enumerable);
@@ -2946,7 +2940,6 @@ slicebefore_i(RB_BLOCK_CALL_FUNC_ARGLIST https://github.com/ruby/ruby/blob/trunk/enum.c#L2940
  *
  *    enum.slice_before(pattern).each { |ary| ... }
  *    enum.slice_before { |elt| bool }.each { |ary| ... }
- *    enum.slice_before(initial_state) { |elt, state| bool }.each { |ary| ... }
  *
  *  Other methods of the Enumerator class and Enumerable module,
  *  such as map, etc., are also usable.
@@ -2992,33 +2985,34 @@ slicebefore_i(RB_BLOCK_CALL_FUNC_ARGLIST https://github.com/ruby/ruby/blob/trunk/enum.c#L2985
  *    }.join(",")
  *    #=> "0,2-4,6,7,9"
  *
- *  However local variables are not appropriate to maintain state
- *  if the result enumerator is used twice or more.
- *  In such a case, the last state of the 1st +each+ is used in the 2nd +each+.
- *  The _initial_state_ argument can be used to avoid this problem.
- *  If non-nil value is given as _initial_state_,
- *  it is duplicated for each +each+ method invocation of the enumerator.
- *  The duplicated object is passed to 2nd argument of the block for
- *  +slice_before+ method.
+ *  However local variables should be used carefully
+ *  if the result enumerator is enumerated twice or more.
+ *  The local variables should be initialized for each enumeration.
+ *  Enumerator.new can be used to do it.
  *
  *    # Word wrapping.  This assumes all characters have same width.
  *    def wordwrap(words, maxwidth)
- *      # if cols is a local variable, 2nd "each" may start with non-zero cols.
- *      words.slice_before(cols: 0) { |w, h|
- *        h[:cols] += 1 if h[:cols] != 0
- *        h[:cols] += w.length
- *        if maxwidth < h[:cols]
- *          h[:cols] = w.length
- *          true
- *        else
- *          false
- *        end
+ *      Enumerator.new {|y|
+ *        # cols is initialized in Enumerator.new.
+ *        cols = 0
+ *        words.slice_before { |w|
+ *          cols += 1 if cols != 0
+ *          cols += w.length
+ *          if maxwidth < cols
+ *            cols = w.length
+ *            true
+ *          else
+ *            false
+ *          end
+ *        }.each {|ws| y.yield ws }
  *      }
  *    end
  *    text = (1..20).to_a.join(" ")
  *    enum = wordwrap(text.split(/\s+/), 10)
  *    puts "-"*10
- *    enum.each { |ws| puts ws.join(" ") }
+ *    enum.each { |ws| puts ws.join(" ") } # first enumeration.
+ *    puts "-"*10
+ *    enum.each { |ws| puts ws.join(" ") } # second enumeration generates same result as the first.
  *    puts "-"*10
  *    #=> ----------
  *    #   1 2 3 4 5
@@ -3028,6 +3022,13 @@ slicebefore_i(RB_BLOCK_CALL_FUNC_ARGLIST https://github.com/ruby/ruby/blob/trunk/enum.c#L3022
  *    #   17 18 19
  *    #   20
  *    #   ----------
+ *    #   1 2 3 4 5
+ *    #   6 7 8 9 10
+ *    #   11 12 13
+ *    #   14 15 16
+ *    #   17 18 19
+ *    #   20
+ *    #   ----------
  *
  *  mbox contains series of mails which start with Unix From line.
  *  So each mail can be extracted by slice before Unix From line.
@@ -3072,7 +3073,7 @@ enum_slice_before(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/enum.c#L3073
         int n;
         n = rb_scan_args(argc, argv, "01", &initial_state);
         if (n != 0)
-	    rb_warn("initial_state given for slice_before.  (Use Enumerator.new with lexical scope variables.)");
+	    rb_warn("initial_state given for slice_before.  (Use local variables.)");
         enumerator = rb_obj_alloc(rb_cEnumerator);
         rb_ivar_set(enumerator, rb_intern("slicebefore_sep_pred"), rb_block_proc());
         rb_ivar_set(enumerator, rb_intern("slicebefore_initial_state"), initial_state);

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

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