Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • enum.c

Enumerable::Enumerator

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

Public Class Methods

Enumerable::Enumerator.new(obj, method = :each, *args) click to toggle source

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

each {...} click to toggle source

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| ... } click to toggle source
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);
}
            
next => object click to toggle source

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);
}
            
rewind => e click to toggle source

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| ... } click to toggle source
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);
}
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus