class Iterator

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]

Public Class Methods

new(obj, *args) { |obj, *args)| ... } click to toggle source

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)

Calls superclass method
# 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