Class Enumerable::Enumerator
In: enumerator.c
lib/generator.rb
Parent: Object

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

Methods

__generator   each   each_with_index   new   next   next   rewind   rewind   with_index  

Included Modules

Enumerable

Public Class methods

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.

[Source]

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

Public Instance methods

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

[Source]

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

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

[Source]

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

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: This feature internally uses Generator, which uses callcc to stop and resume enumeration to fetch each value. Use with care and be aware of the performance loss.

[Source]

     # File lib/generator.rb, line 188
188:   def next
189:     g = __generator
190:     return g.next unless g.end?
191: 
192:     g.rewind
193:     raise StopIteration, 'iteration reached at end' 
194:   end

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.

[Source]

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

Rewinds the enumeration sequence by the next method.

[Source]

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

Rewinds the enumeration sequence by the next method.

[Source]

     # File lib/generator.rb, line 200
200:   def rewind
201:     __generator.rewind
202:     self
203:   end

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

[Source]

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

Private Instance methods

[Source]

     # File lib/generator.rb, line 169
169:   def __generator
170:     @generator ||= Generator.new(self)
171:   end

[Validate]