summaryrefslogtreecommitdiff
path: root/enumerator.c
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-08 14:42:01 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-08 14:42:01 +0000
commit3e11901f1c03f7d3a88349644223f6c920f5f9e2 (patch)
tree16049ef9eed9397783128454832617a0f05258c1 /enumerator.c
parente5f588c83d7ed0ecdbbafe12aa2920c3b5227c3e (diff)
* enumerator.c (enumerator_with_index): receives one argument which
represents a start offset. [ruby-dev:37921] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22134 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enumerator.c')
-rw-r--r--enumerator.c37
1 files changed, 28 insertions, 9 deletions
diff --git a/enumerator.c b/enumerator.c
index d22326ae4a..618bd5eeec 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);