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

ruby-changes:25453

From: marcandre <ko1@a...>
Date: Wed, 7 Nov 2012 02:13:27 +0900 (JST)
Subject: [ruby-changes:25453] marcandRe: r37510 (trunk): * enum.c (enum_cycle): Support for Enumerable#cycle.size

marcandre	2012-11-07 02:13:19 +0900 (Wed, 07 Nov 2012)

  New Revision: 37510

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

  Log:
    * enum.c (enum_cycle): Support for Enumerable#cycle.size
      [Feature #6636]

  Modified files:
    trunk/enum.c
    trunk/test/ruby/test_enumerator.rb

Index: enum.c
===================================================================
--- enum.c	(revision 37509)
+++ enum.c	(revision 37510)
@@ -2235,6 +2235,24 @@
     return Qnil;
 }
 
+static VALUE
+enum_cycle_size(VALUE self, VALUE args)
+{
+    long mul;
+    VALUE n = Qnil;
+    VALUE size = enum_size(self, args);
+
+    if (size == Qnil) return Qnil;
+
+    if (args && (RARRAY_LEN(args) > 0)) {
+	n = RARRAY_PTR(args)[0];
+    }
+    if (n == Qnil) return DBL2NUM(INFINITY);
+    mul = NUM2LONG(n);
+    if (mul <= 0) return INT2FIX(0);
+    return rb_funcall(size, '*', 1, LONG2FIX(mul));
+}
+
 /*
  *  call-seq:
  *     enum.cycle(n=nil) { |obj| block }  ->  nil
@@ -2265,7 +2283,7 @@
 
     rb_scan_args(argc, argv, "01", &nv);
 
-    RETURN_ENUMERATOR(obj, argc, argv);
+    RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_cycle_size);
     if (NIL_P(nv)) {
         n = -1;
     }
Index: test/ruby/test_enumerator.rb
===================================================================
--- test/ruby/test_enumerator.rb	(revision 37509)
+++ test/ruby/test_enumerator.rb	(revision 37510)
@@ -477,6 +477,11 @@
     assert_equal 0,  [:foo, :bar].cycle(-10).size
     assert_equal 0,  [].cycle.size
     assert_equal 0,  [].cycle(5).size
+
+    assert_equal nil, @obj.cycle.size
+    assert_equal nil, @obj.cycle(5).size
+    assert_equal Float::INFINITY, @sized.cycle.size
+    assert_equal 126, @sized.cycle(3).size
   end
 
   def test_size_for_loops

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

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