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

ruby-changes:25461

From: marcandre <ko1@a...>
Date: Wed, 7 Nov 2012 02:15:20 +0900 (JST)
Subject: [ruby-changes:25461] marcandRe: r37518 (trunk): * numeric.c (int_upto, int_downto): Support for Integer#{down|up}to.size

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

  New Revision: 37518

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

  Log:
    * numeric.c (int_upto, int_downto): Support for Integer#{down|up}to.size
      [Feature #6636]

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

Index: numeric.c
===================================================================
--- numeric.c	(revision 37517)
+++ numeric.c	(revision 37518)
@@ -3380,6 +3380,11 @@
     return INT2FIX(sizeof(long));
 }
 
+static VALUE
+int_upto_size(VALUE from, VALUE args) {
+    return num_interval_step_size(from, RARRAY_PTR(args)[0], INT2FIX(1), FALSE);
+}
+
 /*
  *  call-seq:
  *     int.upto(limit) {|i| block }  ->  self
@@ -3400,7 +3405,7 @@
 static VALUE
 int_upto(VALUE from, VALUE to)
 {
-    RETURN_ENUMERATOR(from, 1, &to);
+    RETURN_SIZED_ENUMERATOR(from, 1, &to, int_upto_size);
     if (FIXNUM_P(from) && FIXNUM_P(to)) {
 	long i, end;
 
@@ -3421,6 +3426,11 @@
     return from;
 }
 
+static VALUE
+int_downto_size(VALUE from, VALUE args) {
+    return num_interval_step_size(from, RARRAY_PTR(args)[0], INT2FIX(-1), FALSE);
+}
+
 /*
  *  call-seq:
  *     int.downto(limit) {|i| block }  ->  self
@@ -3442,7 +3452,7 @@
 static VALUE
 int_downto(VALUE from, VALUE to)
 {
-    RETURN_ENUMERATOR(from, 1, &to);
+    RETURN_SIZED_ENUMERATOR(from, 1, &to, int_downto_size);
     if (FIXNUM_P(from) && FIXNUM_P(to)) {
 	long i, end;
 
Index: test/ruby/test_enumerator.rb
===================================================================
--- test/ruby/test_enumerator.rb	(revision 37517)
+++ test/ruby/test_enumerator.rb	(revision 37518)
@@ -543,5 +543,11 @@
     assert_equal Float::INFINITY, (42..Float::INFINITY).step(2).size
     assert_raise(ArgumentError){ (1..10).step(-2).size }
   end
+
+  def test_size_for_downup_to
+    assert_equal 0, 1.upto(-100).size
+    assert_equal 102, 1.downto(-100).size
+    assert_equal Float::INFINITY, 42.upto(Float::INFINITY).size
+  end
 end
 

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

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