summaryrefslogtreecommitdiff
path: root/enum.c
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-06 17:12:50 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-06 17:12:50 +0000
commitdf8451e6062029cb23593679317be97d1c218c7b (patch)
tree64b38d2faa0b56ddcbd43271217f36f683bdf58e /enum.c
parentc82ad6d20781630c9bf71fd811cade770691240e (diff)
* enum.c (enum_each_slice): Support for Enumerable#each_slice.size
[Feature #6636] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37508 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/enum.c b/enum.c
index f9fa993..2f636ed 100644
--- a/enum.c
+++ b/enum.c
@@ -18,8 +18,12 @@
#define STATIC_ASSERT(name, expr) typedef int static_assert_##name##_check[1 - 2*!(expr)]
VALUE rb_mEnumerable;
+
static ID id_next;
+static ID id_div;
static ID id_call;
+static ID id_size;
+
#define id_each idEach
#define id_eqq idEqq
#define id_cmp idCmp
@@ -1778,6 +1782,20 @@ each_slice_i(VALUE i, VALUE m, int argc, VALUE *argv)
return v;
}
+static VALUE
+enum_each_slice_size(VALUE obj, VALUE args)
+{
+ VALUE n, size;
+ long slice_size = NUM2LONG(RARRAY_PTR(args)[0]);
+ if (slice_size <= 0) rb_raise(rb_eArgError, "invalid slice size");
+
+ size = enum_size(obj, 0);
+ if (size == Qnil) return Qnil;
+
+ n = rb_funcall(size, '+', 1, LONG2NUM(slice_size-1));
+ return rb_funcall(n, id_div, 1, LONG2FIX(slice_size));
+}
+
/*
* call-seq:
* enum.each_slice(n) { ... } -> nil
@@ -1802,7 +1820,7 @@ enum_each_slice(VALUE obj, VALUE n)
NODE *memo;
if (size <= 0) rb_raise(rb_eArgError, "invalid slice size");
- RETURN_ENUMERATOR(obj, 1, &n);
+ RETURN_SIZED_ENUMERATOR(obj, 1, &n, enum_each_slice_size);
ary = rb_ary_new2(size);
memo = NEW_MEMO(ary, 0, size);
rb_block_call(obj, id_each, 0, 0, each_slice_i, (VALUE)memo);
@@ -2743,4 +2761,6 @@ Init_Enumerable(void)
id_next = rb_intern("next");
id_call = rb_intern("call");
+ id_size = rb_intern("size");
+ id_div = rb_intern("div");
}