ruby-changes:25448
From: marcandre <ko1@a...>
Date: Wed, 7 Nov 2012 02:12:17 +0900 (JST)
Subject: [ruby-changes:25448] marcandRe: r37505 (trunk): * array.c (rb_ary_cycle): Support for Array#cycle.size
marcandre 2012-11-07 02:12:05 +0900 (Wed, 07 Nov 2012) New Revision: 37505 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37505 Log: * array.c (rb_ary_cycle): Support for Array#cycle.size [Feature #6636] Modified files: trunk/array.c trunk/test/ruby/test_enumerator.rb Index: array.c =================================================================== --- array.c (revision 37504) +++ array.c (revision 37505) @@ -4229,6 +4229,20 @@ return result; } +static VALUE +rb_ary_cycle_size(VALUE self, VALUE args) +{ + long mul; + VALUE n = Qnil; + if (args && (RARRAY_LEN(args) > 0)) { + n = RARRAY_PTR(args)[0]; + } + if (RARRAY_LEN(self) == 0) return INT2FIX(0); + if (n == Qnil) return DBL2NUM(INFINITY); + mul = NUM2LONG(n); + if (mul <= 0) return INT2FIX(0); + return rb_funcall(rb_ary_length(self), '*', 1, LONG2FIX(mul)); +} /* * call-seq: @@ -4258,7 +4272,7 @@ rb_scan_args(argc, argv, "01", &nv); - RETURN_ENUMERATOR(ary, argc, argv); + RETURN_SIZED_ENUMERATOR(ary, argc, argv, rb_ary_cycle_size); if (NIL_P(nv)) { n = -1; } Index: test/ruby/test_enumerator.rb =================================================================== --- test/ruby/test_enumerator.rb (revision 37504) +++ test/ruby/test_enumerator.rb (revision 37505) @@ -456,5 +456,13 @@ (1..59).to_a.repeated_combination(42).size # 1.upto(100).inject(:*) / 1.upto(42).inject(:*) / 1.upto(58).inject(:*) end + + def test_size_for_cycle + assert_equal Float::INFINITY, [:foo].cycle.size + assert_equal 10, [:foo, :bar].cycle(5).size + assert_equal 0, [:foo, :bar].cycle(-10).size + assert_equal 0, [].cycle.size + assert_equal 0, [].cycle(5).size + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/