Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more
A class which provides a method `each' to be used as an Enumerable 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.
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); }
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.
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: 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.
static VALUE enumerator_rewind(obj) VALUE obj; { rb_require("generator"); return rb_funcall(obj, rb_intern("rewind"), 0, 0); }
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); }