| Module | Enumerable |
| In: |
Iterates the given block for each array of consecutive <n> elements.
e.g.:
(1..10).each_cons(3) {|a| p a}
# outputs below
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]
[5, 6, 7]
[6, 7, 8]
[7, 8, 9]
[8, 9, 10]
/*
* call-seq:
* each_cons(n) {...}
*
* Iterates the given block for each array of consecutive <n>
* elements.
*
* e.g.:
* (1..10).each_cons(3) {|a| p a}
* # outputs below
* [1, 2, 3]
* [2, 3, 4]
* [3, 4, 5]
* [4, 5, 6]
* [5, 6, 7]
* [6, 7, 8]
* [7, 8, 9]
* [8, 9, 10]
*
*/
static VALUE
enum_each_cons(obj, n)
VALUE obj, n;
{
long size = NUM2LONG(n);
NODE *memo;
if (size <= 0) rb_raise(rb_eArgError, "invalid size");
memo = rb_node_newnode(NODE_MEMO, rb_ary_new2(size), 0, size);
rb_iterate(rb_each, obj, each_cons_i, (VALUE)memo);
return Qnil;
}
Iterates the given block for each slice of <n> elements.
e.g.:
(1..10).each_slice(3) {|a| p a}
# outputs below
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]
/*
* call-seq:
* e.each_slice(n) {...}
*
* Iterates the given block for each slice of <n> elements.
*
* e.g.:
* (1..10).each_slice(3) {|a| p a}
* # outputs below
* [1, 2, 3]
* [4, 5, 6]
* [7, 8, 9]
* [10]
*
*/
static VALUE
enum_each_slice(obj, n)
VALUE obj, n;
{
long size = NUM2LONG(n);
NODE *memo;
VALUE ary;
if (size <= 0) rb_raise(rb_eArgError, "invalid slice size");
memo = rb_node_newnode(NODE_MEMO, rb_ary_new2(size), 0, size);
rb_iterate(rb_each, obj, each_slice_i, (VALUE)memo);
ary = memo->u1.value;
if (RARRAY(ary)->len > 0) rb_yield(ary);
return Qnil;
}
Returns Enumerable::Enumerator.new(self, :each_slice, n).
/*
* call-seq:
* e.enum_slice(n)
*
* Returns Enumerable::Enumerator.new(self, :each_slice, n).
*
*/
static VALUE
enumerator_enum_slice(obj, n)
VALUE obj, n;
{
return rb_funcall(rb_cEnumerator, id_new, 3, obj, sym_each_slice, n);
}