In article <4abda8e8.1508c00a.5d2b.ffffdf9c / mx.google.com>,
  Nobuyoshi Nakada <nobu / ruby-lang.org> writes:

>> Enumerator.new uses method2 when next method is called if
>> method2 is provided.  obj.method2 should return an object
>> for external iteration. 
>
> Interesting.

method2 should return an object to maintain iterator state,
not return next value directly.

For example, array iterator needs an index as the state.

class Array
  class Iter
    def initialize(ary)
      @ary = ary
      @i = 0
    end
    def next
      raise StopIteratoion if @ary.length <= @i
      @i += 1
      @ary[@i-1]
    end
  end

  def external_iterator_for_each
    Iter.new(self)
  end
end
-- 
Tanaka Akira