ruby-changes:25462
From: marcandre <ko1@a...>
Date: Wed, 7 Nov 2012 02:15:38 +0900 (JST)
Subject: [ruby-changes:25462] marcandRe: r37519 (trunk): * numeric.c (int_dotimes): Support for Integer#times.size
marcandre 2012-11-07 02:15:30 +0900 (Wed, 07 Nov 2012) New Revision: 37519 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37519 Log: * numeric.c (int_dotimes): Support for Integer#times.size [Feature #6636] Modified files: trunk/numeric.c trunk/test/ruby/test_enumerator.rb Index: numeric.c =================================================================== --- numeric.c (revision 37518) +++ numeric.c (revision 37519) @@ -3473,6 +3473,18 @@ return from; } +static VALUE +int_dotimes_size(VALUE num) +{ + if (FIXNUM_P(num)) { + if (NUM2LONG(num) <= 0) return INT2FIX(0); + } + else { + if (RTEST(rb_funcall(num, '<', 1, INT2FIX(0)))) return INT2FIX(0); + } + return num; +} + /* * call-seq: * int.times {|i| block } -> self @@ -3495,7 +3507,7 @@ static VALUE int_dotimes(VALUE num) { - RETURN_ENUMERATOR(num, 0, 0); + RETURN_SIZED_ENUMERATOR(num, 0, 0, int_dotimes_size); if (FIXNUM_P(num)) { long i, end; Index: test/ruby/test_enumerator.rb =================================================================== --- test/ruby/test_enumerator.rb (revision 37518) +++ test/ruby/test_enumerator.rb (revision 37519) @@ -506,6 +506,7 @@ def test_size_for_loops assert_equal Float::INFINITY, loop.size + assert_equal 42, 42.times.size end def test_size_for_each_slice -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/