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

ruby-changes:38093

From: akr <ko1@a...>
Date: Mon, 6 Apr 2015 22:53:40 +0900 (JST)
Subject: [ruby-changes:38093] akr:r50174 (trunk): * enum.c: Enumerable#chunk and Enumerable#slice_before no longer takes

akr	2015-04-06 22:53:24 +0900 (Mon, 06 Apr 2015)

  New Revision: 50174

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

  Log:
    * enum.c: Enumerable#chunk and Enumerable#slice_before no longer takes
      the initial_state argument.  [Feature #10958]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/enum.c
    trunk/test/ruby/test_enum.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50173)
+++ ChangeLog	(revision 50174)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Apr  6 22:52:35 2015  Tanaka Akira  <akr@f...>
+
+	* enum.c: Enumerable#chunk and Enumerable#slice_before no longer takes
+	  the initial_state argument.  [Feature #10958]
+
 Mon Apr  6 16:09:58 2015  Koichi Sasada  <ko1@a...>
 
 	* vm_args.c: protect value stack from calling other methods
Index: enum.c
===================================================================
--- enum.c	(revision 50173)
+++ enum.c	(revision 50174)
@@ -2706,7 +2706,6 @@ enum_cycle(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L2706
 
 struct chunk_arg {
     VALUE categorize;
-    VALUE state;
     VALUE prev_value;
     VALUE prev_elts;
     VALUE yielder;
@@ -2722,10 +2721,7 @@ chunk_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _ https://github.com/ruby/ruby/blob/trunk/enum.c#L2721
 
     ENUM_WANT_SVALUE();
 
-    if (NIL_P(argp->state))
-        v = rb_funcall(argp->categorize, id_call, 1, i);
-    else
-        v = rb_funcall(argp->categorize, id_call, 2, i, argp->state);
+    v = rb_funcall(argp->categorize, id_call, 1, i);
 
     if (v == alone) {
         if (!NIL_P(argp->prev_value)) {
@@ -2771,14 +2767,10 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yield https://github.com/ruby/ruby/blob/trunk/enum.c#L2767
 
     enumerable = rb_ivar_get(enumerator, rb_intern("chunk_enumerable"));
     memo->categorize = rb_ivar_get(enumerator, rb_intern("chunk_categorize"));
-    memo->state = rb_ivar_get(enumerator, rb_intern("chunk_initial_state"));
     memo->prev_value = Qnil;
     memo->prev_elts = Qnil;
     memo->yielder = yielder;
 
-    if (!NIL_P(memo->state))
-	memo->state = rb_obj_dup(memo->state);
-
     rb_block_call(enumerable, id_each, 0, 0, chunk_ii, arg);
     memo = MEMO_FOR(struct chunk_arg, arg);
     if (!NIL_P(memo->prev_elts))
@@ -2789,7 +2781,6 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yield https://github.com/ruby/ruby/blob/trunk/enum.c#L2781
 /*
  *  call-seq:
  *     enum.chunk { |elt| ... }                       -> an_enumerator
- *     enum.chunk(initial_state) { |elt, state| ... } -> an_enumerator (deprecated)
  *
  *  Enumerates over the items, chunking them together based on the return
  *  value of the block.
@@ -2875,22 +2866,16 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yield https://github.com/ruby/ruby/blob/trunk/enum.c#L2866
  *
  */
 static VALUE
-enum_chunk(int argc, VALUE *argv, VALUE enumerable)
+enum_chunk(VALUE enumerable)
 {
-    VALUE initial_state;
     VALUE enumerator;
-    int n;
 
     if (!rb_block_given_p())
 	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 local variables.)");
 
     enumerator = rb_obj_alloc(rb_cEnumerator);
     rb_ivar_set(enumerator, rb_intern("chunk_enumerable"), enumerable);
     rb_ivar_set(enumerator, rb_intern("chunk_categorize"), rb_block_proc());
-    rb_ivar_set(enumerator, rb_intern("chunk_initial_state"), initial_state);
     rb_block_call(enumerator, idInitialize, 0, 0, chunk_i, enumerator);
     return enumerator;
 }
@@ -2899,7 +2884,6 @@ enum_chunk(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L2884
 struct slicebefore_arg {
     VALUE sep_pred;
     VALUE sep_pat;
-    VALUE state;
     VALUE prev_elts;
     VALUE yielder;
 };
@@ -2914,10 +2898,8 @@ slicebefore_ii(RB_BLOCK_CALL_FUNC_ARGLIS https://github.com/ruby/ruby/blob/trunk/enum.c#L2898
 
     if (!NIL_P(argp->sep_pat))
         header_p = rb_funcall(argp->sep_pat, id_eqq, 1, i);
-    else if (NIL_P(argp->state))
-        header_p = rb_funcall(argp->sep_pred, id_call, 1, i);
     else
-        header_p = rb_funcall(argp->sep_pred, id_call, 2, i, argp->state);
+        header_p = rb_funcall(argp->sep_pred, id_call, 1, i);
     if (RTEST(header_p)) {
         if (!NIL_P(argp->prev_elts))
             rb_funcall(argp->yielder, id_lshift, 1, argp->prev_elts);
@@ -2943,13 +2925,9 @@ slicebefore_i(RB_BLOCK_CALL_FUNC_ARGLIST https://github.com/ruby/ruby/blob/trunk/enum.c#L2925
     enumerable = rb_ivar_get(enumerator, rb_intern("slicebefore_enumerable"));
     memo->sep_pred = rb_attr_get(enumerator, rb_intern("slicebefore_sep_pred"));
     memo->sep_pat = NIL_P(memo->sep_pred) ? rb_ivar_get(enumerator, rb_intern("slicebefore_sep_pat")) : Qnil;
-    memo->state = rb_attr_get(enumerator, rb_intern("slicebefore_initial_state"));
     memo->prev_elts = Qnil;
     memo->yielder = yielder;
 
-    if (!NIL_P(memo->state))
-        memo->state = rb_obj_dup(memo->state);
-
     rb_block_call(enumerable, id_each, 0, 0, slicebefore_ii, arg);
     memo = MEMO_FOR(struct slicebefore_arg, arg);
     if (!NIL_P(memo->prev_elts))
@@ -2961,7 +2939,6 @@ slicebefore_i(RB_BLOCK_CALL_FUNC_ARGLIST https://github.com/ruby/ruby/blob/trunk/enum.c#L2939
  *  call-seq:
  *     enum.slice_before(pattern)                             -> an_enumerator
  *     enum.slice_before { |elt| bool }                       -> an_enumerator
- *     enum.slice_before(initial_state) { |elt, state| bool } -> an_enumerator (deprecated)
  *
  *  Creates an enumerator for each chunked elements.
  *  The beginnings of chunks are defined by _pattern_ and the block.
@@ -3107,14 +3084,10 @@ enum_slice_before(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/enum.c#L3084
     VALUE enumerator;
 
     if (rb_block_given_p()) {
-        VALUE initial_state;
-        int n;
-        n = rb_scan_args(argc, argv, "01", &initial_state);
-        if (n != 0)
-	    rb_warn("initial_state given for slice_before.  (Use local variables.)");
+        if (argc != 0)
+            rb_error_arity(argc, 0, 0);
         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);
     }
     else {
         VALUE sep_pat;
@@ -3455,7 +3428,7 @@ Init_Enumerable(void) https://github.com/ruby/ruby/blob/trunk/enum.c#L3428
     rb_define_method(rb_mEnumerable, "drop", enum_drop, 1);
     rb_define_method(rb_mEnumerable, "drop_while", enum_drop_while, 0);
     rb_define_method(rb_mEnumerable, "cycle", enum_cycle, -1);
-    rb_define_method(rb_mEnumerable, "chunk", enum_chunk, -1);
+    rb_define_method(rb_mEnumerable, "chunk", enum_chunk, 0);
     rb_define_method(rb_mEnumerable, "slice_before", enum_slice_before, -1);
     rb_define_method(rb_mEnumerable, "slice_after", enum_slice_after, -1);
     rb_define_method(rb_mEnumerable, "slice_when", enum_slice_when, 0);
Index: NEWS
===================================================================
--- NEWS	(revision 50173)
+++ NEWS	(revision 50174)
@@ -25,6 +25,11 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L25
   * Array#flatten and Array#flatten! no longer try to call #to_ary
     method on elements beyond the given level.  [Bug #10748]
 
+* Enumerable
+  * Enumerable#chunk and Enumerable#slice_before no longer takes the
+    initial_state argument.  [Feature #10958]
+    Use a local variable instead to maintain a state.
+
 * IO
   * IO#close doesn't raise when the IO object is closed.  [Feature #10718]
 
Index: test/ruby/test_enum.rb
===================================================================
--- test/ruby/test_enum.rb	(revision 50173)
+++ test/ruby/test_enum.rb	(revision 50174)
@@ -481,22 +481,6 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L481
     e = @obj.chunk {|elt| elt & 2 == 0 ? false : true }
     assert_equal([[false, [1]], [true, [2, 3]], [false, [1]], [true, [2]]], e.to_a)
 
-    e = @obj.chunk(acc: 0) {|elt, h| h[:acc] += elt; h[:acc].even? }
-    assert_equal([[false, [1,2]], [true, [3]], [false, [1,2]]], e.to_a)
-    assert_equal([[false, [1,2]], [true, [3]], [false, [1,2]]], e.to_a) # this tests h is duplicated.
-
-    hs = [{}]
-    e = [:foo].chunk(hs[0]) {|elt, h|
-      hs << h
-      true
-    }
-    assert_equal([[true, [:foo]]], e.to_a)
-    assert_equal([[true, [:foo]]], e.to_a)
-    assert_equal([{}, {}, {}], hs)
-    assert_not_same(hs[0], hs[1])
-    assert_not_same(hs[0], hs[2])
-    assert_not_same(hs[1], hs[2])
-
     e = @obj.chunk {|elt| elt < 3 ? :_alone : true }
     assert_equal([[:_alone, [1]],
                   [:_alone, [2]],
@@ -526,22 +510,6 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L510
     e = @obj.slice_before {|elt| elt.odd? }
     assert_equal([[1,2], [3], [1,2]], e.to_a)
 
-    e = @obj.slice_before(acc: 0) {|elt, h| h[:acc] += elt; h[:acc].even? }
-    assert_equal([[1,2], [3,1,2]], e.to_a)
-    assert_equal([[1,2], [3,1,2]], e.to_a) # this tests h is duplicated.
-
-    hs = [{}]
-    e = [:foo].slice_before(hs[0]) {|elt, h|
-      hs << h
-      true
-    }
-    assert_equal([[:foo]], e.to_a)
-    assert_equal([[:foo]], e.to_a)
-    assert_equal([{}, {}, {}], hs)
-    assert_not_same(hs[0], hs[1])
-    assert_not_same(hs[0], hs[2])
-    assert_not_same(hs[1], hs[2])
-
     ss = %w[abc defg h ijk l mno pqr st u vw xy z]
     assert_equal([%w[abc defg h], %w[ijk l], %w[mno], %w[pqr st u vw xy z]],
                  ss.slice_before(/\A...\z/).to_a)

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

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