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

ruby-changes:5127

From: knu <ko1@a...>
Date: Tue, 27 May 2008 13:15:59 +0900 (JST)
Subject: [ruby-changes:5127] Ruby:r16622 (trunk): * enum.c (enum_to_a): Pass arguments through to #each().

knu	2008-05-27 13:15:44 +0900 (Tue, 27 May 2008)

  New Revision: 16622

  Modified files:
    trunk/ChangeLog
    trunk/enum.c

  Log:
    * enum.c (enum_to_a): Pass arguments through to #each().
      (enum_sort): Follow the enum_to_a signature change.
      (enum_reverse_each): Add #reverse_each().


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=16622&r2=16621&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/enum.c?r1=16622&r2=16621&diff_format=u

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 16621)
+++ ChangeLog	(revision 16622)
@@ -1,3 +1,9 @@
+Tue May 27 13:14:53 2008  Akinori MUSHA  <knu@i...>
+
+	* enum.c (enum_to_a): Pass arguments through to #each().
+	  (enum_sort): Follow the enum_to_a signature change.
+	  (enum_reverse_each): Add #reverse_each().
+
 Tue May 27 13:12:37 2008  Akinori MUSHA  <knu@i...>
 
 	* io.c (Init_IO): Define ARGF.{lines,bytes,chars}.
Index: enum.c
===================================================================
--- enum.c	(revision 16621)
+++ enum.c	(revision 16622)
@@ -383,11 +383,11 @@
  *     { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a   #=> [["a", 1], ["b", 2], ["c", 3]]
  */
 static VALUE
-enum_to_a(VALUE obj)
+enum_to_a(int argc, VALUE *argv, VALUE obj)
 {
     VALUE ary = rb_ary_new();
 
-    rb_block_call(obj, id_each, 0, 0, collect_all, ary);
+    rb_block_call(obj, id_each, argc, argv, collect_all, ary);
 
     return ary;
 }
@@ -657,7 +657,7 @@
 static VALUE
 enum_sort(VALUE obj)
 {
-    return rb_ary_sort(enum_to_a(obj));
+    return rb_ary_sort(enum_to_a(0, 0, obj));
 }
 
 static VALUE
@@ -1404,7 +1404,32 @@
 }
 
 
+/*
+ *  call-seq:
+ *     enum.reverse_each {|item| block } 
+ *  
+ *  Traverses <i>enum</i> in reverse order.
+ */
+
 static VALUE
+enum_reverse_each(int argc, VALUE *argv, VALUE obj)
+{
+    VALUE ary;
+    long i;
+
+    RETURN_ENUMERATOR(obj, argc, argv);
+
+    ary = enum_to_a(argc, argv, obj);
+
+    for (i = RARRAY_LEN(ary); --i >= 0; ) {
+	rb_yield(RARRAY_PTR(ary)[i]);
+    }
+
+    return obj;
+}
+
+
+static VALUE
 zip_ary(VALUE val, NODE *memo, int argc, VALUE *argv)
 {
     volatile VALUE result = memo->u1.value;
@@ -1756,8 +1781,8 @@
 {
     rb_mEnumerable = rb_define_module("Enumerable");
 
-    rb_define_method(rb_mEnumerable, "to_a", enum_to_a, 0);
-    rb_define_method(rb_mEnumerable, "entries", enum_to_a, 0);
+    rb_define_method(rb_mEnumerable, "to_a", enum_to_a, -1);
+    rb_define_method(rb_mEnumerable, "entries", enum_to_a, -1);
 
     rb_define_method(rb_mEnumerable, "sort", enum_sort, 0);
     rb_define_method(rb_mEnumerable, "sort_by", enum_sort_by, 0);
@@ -1789,6 +1814,7 @@
     rb_define_method(rb_mEnumerable, "member?", enum_member, 1);
     rb_define_method(rb_mEnumerable, "include?", enum_member, 1);
     rb_define_method(rb_mEnumerable, "each_with_index", enum_each_with_index, -1);
+    rb_define_method(rb_mEnumerable, "reverse_each", enum_reverse_each, -1);
     rb_define_method(rb_mEnumerable, "zip", enum_zip, -1);
     rb_define_method(rb_mEnumerable, "take", enum_take, 1);
     rb_define_method(rb_mEnumerable, "take_while", enum_take_while, 0);

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

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