summaryrefslogtreecommitdiff
path: root/enum.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-10-30 01:06:10 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-10-30 01:06:10 +0000
commita68451d61015e094d804d286027ac22d443aab62 (patch)
tree2a4c067e0738370e9d12c53a417def4ce36fccc8 /enum.c
parentf3cbb20b2272ea5b1c9cafb16e22221aa527f952 (diff)
* 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: svn+ssh://ci.ruby-lang.org/ruby/trunk@13793 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/enum.c b/enum.c
index 82a64fe0a3..2c4a4e947b 100644
--- a/enum.c
+++ b/enum.c
@@ -1331,6 +1331,43 @@ enum_each_with_index(int argc, VALUE *argv, VALUE 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.
+ * <i>n</i> 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)
{
volatile VALUE result = memo->u1.value;
@@ -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);