ruby-changes:10578
From: mame <ko1@a...>
Date: Sun, 8 Feb 2009 23:42:15 +0900 (JST)
Subject: [ruby-changes:10578] Ruby:r22134 (trunk): * enumerator.c (enumerator_with_index): receives one argument which
mame 2009-02-08 23:42:01 +0900 (Sun, 08 Feb 2009) New Revision: 22134 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=22134 Log: * enumerator.c (enumerator_with_index): receives one argument which represents a start offset. [ruby-dev:37921] Modified files: trunk/ChangeLog trunk/enumerator.c trunk/test/ruby/test_enumerator.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 22133) +++ ChangeLog (revision 22134) @@ -1,3 +1,8 @@ +Sun Feb 8 23:37:17 2009 Yusuke Endoh <mame@t...> + + * enumerator.c (enumerator_with_index): receives one argument which + represents a start offset. [ruby-dev:37921] + Sun Feb 8 23:28:05 2009 Yusuke Endoh <mame@t...> * include/ruby/st.h, st.c: order entries by a linked list instead of Index: enumerator.c =================================================================== --- enumerator.c (revision 22133) +++ enumerator.c (revision 22134) @@ -406,32 +406,51 @@ /* * call-seq: - * e.with_index {|(*args), idx| ... } + * e.with_index(offset = 0) {|(*args), idx| ... } * e.with_index * * Iterates the given block for each element with an index, which - * start from 0. If no block is given, returns an enumerator. + * starts from +offset+. If no block is given, returns an enumerator. * */ static VALUE -enumerator_with_index(VALUE obj) +enumerator_with_index(int argc, VALUE *argv, VALUE obj) { struct enumerator *e; - VALUE memo = 0; - int argc = 0; - VALUE *argv = 0; + VALUE memo; - RETURN_ENUMERATOR(obj, 0, 0); + rb_scan_args(argc, argv, "01", &memo); + RETURN_ENUMERATOR(obj, argc, argv); + memo = NIL_P(memo) ? 0 : (VALUE)NUM2LONG(memo); e = enumerator_ptr(obj); if (e->args) { argc = RARRAY_LEN(e->args); argv = RARRAY_PTR(e->args); } + else { + argc = 0; + argv = NULL; + } return rb_block_call(e->obj, e->meth, argc, argv, enumerator_with_index_i, (VALUE)&memo); } +/* + * call-seq: + * e.each_with_index {|(*args), idx| ... } + * e.each_with_index + * + * Same as Enumeartor#with_index, except each_with_index does not + * receive an offset argument. + * + */ static VALUE +enumerator_each_with_index(VALUE obj) +{ + return enumerator_with_index(0, NULL, obj); +} + +static VALUE enumerator_with_object_i(VALUE val, VALUE memo) { return rb_yield_values(2, val, memo); @@ -841,9 +860,9 @@ rb_define_method(rb_cEnumerator, "initialize", enumerator_initialize, -1); rb_define_method(rb_cEnumerator, "initialize_copy", enumerator_init_copy, 1); rb_define_method(rb_cEnumerator, "each", enumerator_each, 0); - rb_define_method(rb_cEnumerator, "each_with_index", enumerator_with_index, 0); + rb_define_method(rb_cEnumerator, "each_with_index", enumerator_each_with_index, 0); rb_define_method(rb_cEnumerator, "each_with_object", enumerator_with_object, 1); - rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, 0); + rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, -1); rb_define_method(rb_cEnumerator, "with_object", enumerator_with_object, 1); rb_define_method(rb_cEnumerator, "next", enumerator_next, 0); rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0); Index: test/ruby/test_enumerator.rb =================================================================== --- test/ruby/test_enumerator.rb (revision 22133) +++ test/ruby/test_enumerator.rb (revision 22134) @@ -94,6 +94,7 @@ def test_with_index assert_equal([[1,0],[2,1],[3,2]], @obj.to_enum(:foo, 1, 2, 3).with_index.to_a) + assert_equal([[1,5],[2,6],[3,7]], @obj.to_enum(:foo, 1, 2, 3).with_index(5).to_a) end def test_with_object -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/