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

ruby-changes:7345

From: knu <ko1@a...>
Date: Tue, 26 Aug 2008 14:47:21 +0900 (JST)
Subject: [ruby-changes:7345] Ruby:r18864 (trunk): * enumerator.c: Activate Enumerator#with_object and add

knu	2008-08-26 14:45:18 +0900 (Tue, 26 Aug 2008)

  New Revision: 18864

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

  Log:
    * enumerator.c: Activate Enumerator#with_object and add
      Enumerable#each_with_object.

  Modified files:
    trunk/ChangeLog
    trunk/doc/NEWS
    trunk/enumerator.c

Index: doc/NEWS
===================================================================
--- doc/NEWS	(revision 18863)
+++ doc/NEWS	(revision 18864)
@@ -111,6 +111,8 @@
     * Enumerable and Enumerator
           o Enumerable#map,collect_all called without a block returns
             an enumerator.
+          o Enumerable#each_with_object [experimental]
+          o Enumerator#with_object [experimental]
           o Enumerator.new { ... } [experimental]
     * Regexp#match, String#match
           o Regexp#match, String#match
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18863)
+++ ChangeLog	(revision 18864)
@@ -1,3 +1,8 @@
+Tue Aug 26 14:43:10 2008  Akinori MUSHA  <knu@i...>
+
+	* enumerator.c: Activate Enumerator#with_object and add
+	  Enumerable#each_with_object.
+
 Tue Aug 26 14:38:32 2008  Akinori MUSHA  <knu@i...>
 
 	* enumerator.c (enumerator_initialize),
Index: enumerator.c
===================================================================
--- enumerator.c	(revision 18863)
+++ enumerator.c	(revision 18864)
@@ -217,6 +217,37 @@
 }
 
 static VALUE
+each_with_object_i(VALUE val, VALUE memo)
+{
+    return rb_yield_values(2, val, memo);
+}
+
+/*
+ *  call-seq:
+ *    each_with_object(obj) {|(*args), memo_obj| ... }
+ *    each_with_object(obj)
+ *
+ *  Iterates the given block for each element with an arbitrary
+ *  object given, and returns the initially given object.
+
+ *  If no block is given, returns an enumerator.
+ *
+ *  e.g.:
+ *      evens = (1..10).each_with_object([]) {|i, a| a << i*2 }
+ *      # => [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
+ *
+ */
+static VALUE
+enum_each_with_object(VALUE obj, VALUE memo)
+{
+    RETURN_ENUMERATOR(obj, 1, &memo);
+
+    rb_block_call(obj, SYM2ID(sym_each), 0, 0, each_with_object_i, memo);
+
+    return memo;
+}
+
+static VALUE
 enumerator_allocate(VALUE klass)
 {
     struct enumerator *ptr;
@@ -412,7 +443,7 @@
  *    e.with_object(obj)
  *
  *  Iterates the given block for each element with an arbitrary
- *  object given, and returns memo object.
+ *  object given, and returns the initially given object.
  *
  *  If no block is given, returns an enumerator.
  *
@@ -424,7 +455,7 @@
     int argc = 0;
     VALUE *argv = 0;
 
-    RETURN_ENUMERATOR(obj, 0, 0);
+    RETURN_ENUMERATOR(obj, 1, &memo);
     e = enumerator_ptr(obj);
     if (e->args) {
 	argc = RARRAY_LEN(e->args);
@@ -734,6 +765,7 @@
 
     rb_define_method(rb_mEnumerable, "each_slice", enum_each_slice, 1);
     rb_define_method(rb_mEnumerable, "each_cons", enum_each_cons, 1);
+    rb_define_method(rb_mEnumerable, "each_with_object", enum_each_with_object, 1);
 
     rb_cEnumerator = rb_define_class("Enumerator", rb_cObject);
     rb_include_module(rb_cEnumerator, rb_mEnumerable);
@@ -743,12 +775,9 @@
     rb_define_method(rb_cEnumerator, "initialize_copy", enumerator_init_copy, 1);
     rb_define_method(rb_cEnumerator, "each", enumerator_each, 0);
     rb_define_method(rb_cEnumerator, "each_with_index", enumerator_with_index, 0);
+    rb_define_method(rb_cEnumerator, "each_with_object", enumerator_with_object, 1);
     rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, 0);
-#if 0
     rb_define_method(rb_cEnumerator, "with_object", enumerator_with_object, 1);
-#else
-    (void)enumerator_with_object;
-#endif
     rb_define_method(rb_cEnumerator, "next", enumerator_next, 0);
     rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0);
 

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

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