From fd8c8d09f41f9e83ed56efc8feaa1fe612ff90fc Mon Sep 17 00:00:00 2001 From: knu Date: Wed, 9 Apr 2008 11:13:04 +0000 Subject: * enumerator.c, inits.c (rb_call_inits), ruby.h, intern.h, ext/enumerator, common.mk (OBJS, enumerator.$(OBJEXT)): Make the enumerator module built-in, * enumerator.c: New method: Enumerable::Enumerator#with_index. * enum.c (enum_each_with_index): Enumerable#each_with_index now returns an enumerator instead of raising an exception if no block is given. Enumerable#enum_with_index, formerly defined in the enumerator module, is kept as an alias to each_with_index for backward compatibility. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@15934 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- enum.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'enum.c') diff --git a/enum.c b/enum.c index de2278c333..a09e3f0a7e 100644 --- a/enum.c +++ b/enum.c @@ -17,6 +17,41 @@ VALUE rb_mEnumerable; static ID id_each, id_eqq, id_cmp; +struct iter_method_arg { + VALUE obj; + ID mid; + int argc; + VALUE *argv; +}; + +static VALUE +iterate_method(obj) + VALUE obj; +{ + struct iter_method_arg *arg; + + arg = (struct iter_method_arg *)obj; + return rb_funcall2(arg->obj, arg->mid, arg->argc, arg->argv); +} + +VALUE +rb_block_call(obj, mid, argc, argv, bl_proc, data2) + VALUE obj; + ID mid; + int argc; + VALUE *argv; + VALUE (*bl_proc) (ANYARGS); + VALUE data2; +{ + struct iter_method_arg arg; + + arg.obj = obj; + arg.mid = mid; + arg.argc = argc; + arg.argv = argv; + return rb_iterate(iterate_method, (VALUE)&arg, bl_proc, data2); +} + VALUE rb_each(obj) VALUE obj; @@ -813,9 +848,11 @@ static VALUE enum_each_with_index(obj) VALUE obj; { - VALUE memo = 0; + VALUE memo; + + RETURN_ENUMERATOR(obj, 0, 0); - rb_need_block(); + memo = 0; rb_iterate(rb_each, obj, each_with_index_i, (VALUE)&memo); return obj; } @@ -928,6 +965,7 @@ Init_Enumerable() rb_define_method(rb_mEnumerable,"member?", enum_member, 1); rb_define_method(rb_mEnumerable,"include?", enum_member, 1); rb_define_method(rb_mEnumerable,"each_with_index", enum_each_with_index, 0); + rb_define_method(rb_mEnumerable,"enum_with_index", enum_each_with_index, 0); rb_define_method(rb_mEnumerable, "zip", enum_zip, -1); id_eqq = rb_intern("==="); -- cgit v1.2.3