diff options
| author | mame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-02-08 14:42:01 +0000 |
|---|---|---|
| committer | mame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-02-08 14:42:01 +0000 |
| commit | e2494d17a06bf5a2eeb8963b1fe981b341abe3b1 (patch) | |
| tree | 16049ef9eed9397783128454832617a0f05258c1 /enumerator.c | |
| parent | e46c459c04d30bbca0c8346fb253e5b42664b9ad (diff) | |
| download | ruby-e2494d17a06bf5a2eeb8963b1fe981b341abe3b1.tar.gz ruby-e2494d17a06bf5a2eeb8963b1fe981b341abe3b1.tar.xz ruby-e2494d17a06bf5a2eeb8963b1fe981b341abe3b1.zip | |
* enumerator.c (enumerator_with_index): receives one argument which
represents a start offset. [ruby-dev:37921]
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@22134 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enumerator.c')
| -rw-r--r-- | enumerator.c | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/enumerator.c b/enumerator.c index d22326ae4..618bd5eee 100644 --- a/enumerator.c +++ b/enumerator.c @@ -406,31 +406,50 @@ enumerator_with_index_i(VALUE val, VALUE *memo) /* * 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) { @@ -841,9 +860,9 @@ Init_Enumerator(void) 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); |
