Class: Iterator
- Inherits:
-
Enumerator
- Object
- Enumerator
- Iterator
- Defined in:
- lib/mug/iterator.rb
Overview
A special class of Enumerator that repeatedly yields values to a block.
The initial yielded value is given in the constructor, but in subsequent iterations the result of the previous iteration is yielded.
Example:
0.iter_for(:next).take(5) #=> [1,2,3,4,5]
Instance Method Summary collapse
-
#initialize(obj, *args) ⇒ Iterator
constructor
Creates a new Iterator object, which can be used as an Enumerable.
Methods inherited from Enumerator
Constructor Details
#initialize(obj, *args) ⇒ Iterator
Creates a new Iterator object, which can be used as an Enumerable.
In the first form, iteration is defined by the given block, to which the current object and any other args
are yielded.
In the second, deprecated, form, a generated Iterator sends the given method with any args
to the iterand.
Use of this form is discouraged. Use Object#iter_for or Method#to_iter instead.
@call-seq new(initial, *args) { |obj, *args| … } @call-seq new(initial, method=:each, *args)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mug/iterator.rb', line 31 def initialize obj, *args if block_given? super() do |y| loop do y << (obj = yield obj, *args) end end else warn 'Iterator.new without a block is deprecated; use Object#to_iter' args = [:each] if args.empty? super() do |y| loop do y << (obj = obj.send(*args)) end end end end |