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.
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.
/* * 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); }
Iterates the given block using the object and the method specified in the first place. If no block is given, returns self.
/* * 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.
/* * 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.
# 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.
/* * 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); }
Iterates the given block for each elements with an index, which start from 0. If no block is given, returns an enumerator.
/* * 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); }