Jacob Fugal
2/16/2006 5:24:00 PM
On 2/16/06, Ruby Quiz <james@grayproductions.net> wrote:
> Let's see how Jacob Fugal implemented that (with a small patch from Ross
> Bamford). Here's the setup:
<snip>
> Here are the rest of the methods for Jacob's solution:
<snip>
> 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