Class: Enumerable::Enumerator
- Inherits:
-
Object
- Object
- Enumerable::Enumerator
- Defined in:
- lib/generator.rb
Instance Method Summary collapse
-
#next ⇒ Object
call-seq: e.next => object.
-
#rewind ⇒ Object
call-seq: e.rewind => e.
Instance Method Details
#next ⇒ Object
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: 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.
188 189 190 191 192 193 194 |
# File 'lib/generator.rb', line 188 def next g = __generator return g.next unless g.end? g.rewind raise StopIteration, 'iteration reached at end' end |
#rewind ⇒ Object
call-seq:
e.rewind => e
Rewinds the enumeration sequence by the next method.
200 201 202 203 |
# File 'lib/generator.rb', line 200 def rewind __generator.rewind self end |