From: Jacob Fugal Date: 2006-02-17T02:23:35+09:00 Subject: Re: [SUMMARY] FasterGenerator (#66) On 2/16/06, Ruby Quiz wrote: > Let's see how Jacob Fugal implemented that (with a small patch from Ross > Bamford). Here's the setup: > Here are the rest of the methods for Jacob's solution: > def rewind > @position = 0 > self > end One important thing: I've since realized that this implementation of rewind was flawed. It should be something like: def rewind if @thread @thread.kill if @thread.status @thread = nil end @values = [] @position = 0 @done = false end My initial rewind just reset to the beginning of the list of generated values, but for proper operation the list itself and the generating thread should also be reset. @done needs to be reset as well, since the previous run of @thread may have completed and marked @done as true, and we definitely don't want that. I've got the if statement around the operations on @thread since I don't want subsequent calls to rewind to cause a NoMethodError on nil. Given this structure in #rewind, we can also refactor #initialize a bit: def initialize( enum=nil, &block ) self.rewind @mutex = Mutex.new @block = block if enum @block = proc{ |g| enum.each{ |x| g.yield x } } end end Jacob Fugal