Class: Enumerable::Enumerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
enumerator.c

Overview

A class which provides a method 'each' to be used as an Enumerable object.

Instance Method Summary collapse

Methods included from Enumerable

#all?, #any?, #collect, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_slice, #entries, #enum_cons, #enum_slice, #enum_with_index, #find, #find_all, #find_index, #first, #grep, #group_by, #include?, #inject, #map, #max, #max_by, #member?, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reduce, #reject, #reverse_each, #select, #sort, #sort_by, #take, #take_while, #to_a, #zip

Constructor Details

#Enumerable::Enumerator.new(obj, method = :each, *args) ⇒ Object

Creates a new Enumerable::Enumerator object, which is to be used as an Enumerable object using the given object's given method with the given arguments.

Use of this method is discouraged. Use Kernel#enum_for() instead.



# File 'enumerator.c'

/*
 *  call-seq:
 *    Enumerable::Enumerator.new(obj, method = :each, *args)
 *
 *  Creates a new Enumerable::Enumerator object, which is to be
 *  used as an Enumerable object using the given object's given
 *  method with the given arguments.
 *
 *  Use of this method is discouraged.  Use Kernel#enum_for() instead.
 */
static VALUE
enumerator_initialize(argc, argv, obj)
    int argc;
    VALUE *argv;
    VALUE obj;
{
    VALUE recv, meth = sym_each;

    if (argc == 0)
    rb_raise(rb_eArgError, "wrong number of argument (0 for 1)");
    recv = *argv++;
    if (--argc) {
    meth = *argv++;
    --argc;
    }
    return enumerator_init(obj, recv, meth, argc, argv);
}

Instance Method Details

#each { ... } ⇒ Object

Iterates the given block using the object and the method specified in the first place. If no block is given, returns self.

Yields:



# File 'enumerator.c'

/*
 *  call-seq:
 *    enum.each {...}
 *
 *  Iterates the given block using the object and the method specified
 *  in the first place.  If no block is given, returns self.
 *
 */
static VALUE
enumerator_each(obj)
    VALUE obj;
{
    struct enumerator *e;
    int argc = 0;
    VALUE *argv = 0;

    if (!rb_block_given_p()) return obj;
    e = enumerator_ptr(obj);
    if (e->args) {
    argc = RARRAY_LEN(e->args);
    argv = RARRAY_PTR(e->args);
    }
    return rb_block_call(e->obj, e->meth, argc, argv, e->iter, (VALUE)e);
}

#with_index {|(*args), idx| ... } ⇒ Object #with_indexObject

Iterates the given block for each elements with an index, which start from 0. If no block is given, returns an enumerator.

Overloads:

  • #with_index {|(*args), idx| ... } ⇒ Object

    Yields:

    • ((*args), idx)


# File 'enumerator.c'

/*
 *  call-seq:
 *    e.with_index {|(*args), idx| ... }
 *    e.with_index
 *
 *  Iterates the given block for each elements with an index, which
 *  start from 0.  If no block is given, returns an enumerator.
 *
 */
static VALUE
enumerator_with_index(obj)
    VALUE obj;
{
    struct enumerator *e = enumerator_ptr(obj);
    VALUE memo = 0;
    int argc = 0;
    VALUE *argv = 0;

    RETURN_ENUMERATOR(obj, 0, 0);
    if (e->args) {
    argc = RARRAY_LEN(e->args);
    argv = RARRAY_PTR(e->args);
    }
    return rb_block_call(e->obj, e->meth, argc, argv,
             enumerator_with_index_i, (VALUE)&memo);
}

#initialize_copyObject

:nodoc:



# File 'enumerator.c'

/* :nodoc: */
static VALUE
enumerator_init_copy(obj, orig)
    VALUE obj;
    VALUE orig;
{
    struct enumerator *ptr0, *ptr1;

    ptr0 = enumerator_ptr(orig);

    Data_Get_Struct(obj, struct enumerator, ptr1);

    if (!ptr1) {
    rb_raise(rb_eArgError, "unallocated enumerator");
    }

    ptr1->obj  = ptr0->obj;
    ptr1->meth = ptr0->meth;
    ptr1->iter = ptr0->iter;
    ptr1->args = ptr0->args;

    return obj;
}

#nextObject

Returns the next object in the enumerator, and move the internal position forward. When the position reached at the end, internal position is rewinded then StopIteration is raised.

Note that enumeration sequence by next method does not affect other non-external enumeration methods, unless underlying iteration methods itself has side-effect, e.g. IO#each_line.

Caution: Calling this method causes the "generator" library to be loaded.

Returns:



# File 'enumerator.c'

/*
 * call-seq:
 *   e.next   => object
 *
 * Returns the next object in the enumerator, and move the internal
 * position forward.  When the position reached at the end, internal
 * position is rewinded then StopIteration is raised.
 *
 * Note that enumeration sequence by next method does not affect other
 * non-external enumeration methods, unless underlying iteration
 * methods itself has side-effect, e.g. IO#each_line.
 *
 * Caution: Calling this method causes the "generator" library to be
 * loaded.
 */

static VALUE
enumerator_next(obj)
    VALUE obj;
{
    rb_require("generator");
    return rb_funcall(obj, rb_intern("next"), 0, 0);
}

#rewindObject

Rewinds the enumeration sequence by the next method.



# File 'enumerator.c'

/*
 * call-seq:
 *   e.rewind   => e
 *
 * Rewinds the enumeration sequence by the next method.
 */

static VALUE
enumerator_rewind(obj)
    VALUE obj;
{
    rb_require("generator");
    return rb_funcall(obj, rb_intern("rewind"), 0, 0);
}

#with_index {|(*args), idx| ... } ⇒ Object #with_indexObject

Iterates the given block for each elements with an index, which start from 0. If no block is given, returns an enumerator.

Overloads:

  • #with_index {|(*args), idx| ... } ⇒ Object

    Yields:

    • ((*args), idx)


# File 'enumerator.c'

/*
 *  call-seq:
 *    e.with_index {|(*args), idx| ... }
 *    e.with_index
 *
 *  Iterates the given block for each elements with an index, which
 *  start from 0.  If no block is given, returns an enumerator.
 *
 */
static VALUE
enumerator_with_index(obj)
    VALUE obj;
{
    struct enumerator *e = enumerator_ptr(obj);
    VALUE memo = 0;
    int argc = 0;
    VALUE *argv = 0;

    RETURN_ENUMERATOR(obj, 0, 0);
    if (e->args) {
    argc = RARRAY_LEN(e->args);
    argv = RARRAY_PTR(e->args);
    }
    return rb_block_call(e->obj, e->meth, argc, argv,
             enumerator_with_index_i, (VALUE)&memo);
}