Module: Enumerable

Module | Enumerable
In: |

### Methods

each_cons each_slice enum_cons enum_slice enum_with_index

### Public Instance methods

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]

[Source]

    /*
     *  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;
    }

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]

[Source]

    /*
     *  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;
    }

e.enum_cons(n)

Returns Enumerable::Enumerator.new(self, :each_cons, n).

[Source]

    /*
     *  call-seq:
     *    e.enum_cons(n)
     *
     *  Returns Enumerable::Enumerator.new(self, :each_cons, n).
     *
     */
    static VALUE
    enumerator_enum_cons(obj, n)
        VALUE obj, n;
    {
        return rb_funcall(rb_cEnumerator, id_new, 3, obj, sym_each_cons, n);
    }

e.enum_slice(n)

Returns Enumerable::Enumerator.new(self, :each_slice, n).

[Source]

    /*
     *  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);
    }

enum_with_index

Returns Enumerable::Enumerator.new(self, :each_with_index).

[Source]

    /*
     *  call-seq:
     *    enum_with_index
     *
     *  Returns Enumerable::Enumerator.new(self, :each_with_index).
     *
     */
    static VALUE
    enumerator_enum_with_index(obj)
        VALUE obj;
    {
        return rb_funcall(rb_cEnumerator, id_new, 2, obj, sym_each_with_index);
    }

[Validate]