From c118625b85c85d844b2b42797e676cfc6972f486 Mon Sep 17 00:00:00 2001 From: matz Date: Tue, 30 Oct 2007 01:06:10 +0000 Subject: * enum.c (enum_butfirst): add a new method to iterates over elements but first n. RDoc need to be updated. * enumerator.c (Init_Enumerator): remove unnecessary symbol initialization. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@13793 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- enum.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'enum.c') diff --git a/enum.c b/enum.c index 82a64fe0a..2c4a4e947 100644 --- a/enum.c +++ b/enum.c @@ -1330,6 +1330,43 @@ enum_each_with_index(int argc, VALUE *argv, VALUE obj) return obj; } +static VALUE +butfirst_i(VALUE val, long *n) +{ + + if (*n > 0) { + (*n)--; + return Qnil; + } + else { + return rb_yield(val); + } +} + +/* + * call-seq: + * e.butfirst {|x| ... } + * e.butfirst(n) {|x| ... } + * + * Iterates the given block for each elements except for first n elements. + * n defaults to 1. + * + */ +static VALUE +enum_butfirst(int argc, VALUE *argv, VALUE obj) +{ + VALUE tmp; + long n; + + rb_scan_args(argc, argv, "01", &tmp); + RETURN_ENUMERATOR(obj, argc, argv); + if (argc == 0) n = 1; + else n = NUM2LONG(tmp); + + rb_block_call(obj, id_each, 0, 0, butfirst_i, (VALUE)&n); + return obj; +} + static VALUE zip_i(VALUE val, NODE *memo) { @@ -1606,6 +1643,7 @@ Init_Enumerable(void) 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, -1); + rb_define_method(rb_mEnumerable, "butfirst", enum_butfirst, -1); rb_define_method(rb_mEnumerable, "zip", enum_zip, -1); rb_define_method(rb_mEnumerable, "take", enum_take, -1); rb_define_method(rb_mEnumerable, "drop", enum_drop, -1); -- cgit