From: Paul Brannan Date: 2003-03-26T00:34:45+09:00 Subject: Re: External iterators all nice and neat (was: Re: Iterate over two lists in parallel) On Tue, Mar 25, 2003 at 05:43:11PM +0900, Julian Snitow wrote: > Apparently for some reason "#{iterator.get}" instead of using a static > expands weirdly in that loop. (Ruby 1.6.8) > > /me nibbles the brim of his fedora (not quite hat-eating-worthy, but > still... it's good to be humbled every now and then... ) > > I'm wondering what's inside the string-expansion that changes the > behavior like that... I think the problem is that you are returning directly from your continution. The first time get() is called, you create a continuation and store it in @cont. When you return from the continuation, you always return to the place where get() was first called. It's a little clearer if you unroll your loop: class Array include Iteration end foo = [1, 2, 3] iterator = foo.iter(false) p iterator.get() #=> 1 p iterator.get() #=> 2 3 nil 1 iterator.get() gets called, returns 1, then 1 gets printed. iterator.get() is called on the next line, then is returned to the previous line, and 2 is printed. This continues until iteration is complete and nil is returned. I think this is why most other parallel iterators are implemented using two continuations instead of one. Paul