On Feb 12, 2006, at 19:23 , James Edward Gray II wrote: > On Feb 12, 2006, at 11:10 AM, Jacob Fugal wrote: > >> Ok, since last night I've corrected my implementation to not evaluate >> ahead, as per H. Yamamoto's and Luke Blanshard's comments. > > Mine does, of course, pull all the items in at once and has all the > problems discussed with that approach. Attached is my > implementation and the altered versions I have been using in > benchmarks. > > James Edward Gray II Mine looks like a clone of James': class MyGenerator attr_reader :index def initialize(enum = nil) if enum then @array = enum.to_a else @array = Array.new yield self end @index = 0 end def current raise EOFError unless next? @array[@index] end def next value = current @index += 1 return value end def next? return @index < @array.length end def rewind @index = 0 self end def each(&block) @array.each(&block) end def yield(value) @array << value end def pos return @index end def end? return !next? end end /Christoffer