Class

Enumerable::Enumerator

Inheritance
< Object
Included Modules
Enumerable

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

Methods

Class

Visibility Signature
public new (...)

Instance

Visibility Signature
public each ()
public each_with_index ()
public next ()
public next ()
public rewind ()
public rewind ()
public with_index ()

Class Method Detail

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 not discouraged. Use Kernel#enum_for() instead.

Instance Method Detail

enum.each {...}

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

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.

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: 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.

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.

e.rewind => e

Rewinds the enumeration sequence by the next method.

e.rewind => e

Rewinds the enumeration sequence by the next method.

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.