# Iterator class not working

**URL:** <https://rubytalk.org/t/iterator-class-not-working/26071>\
**Category:** ruby-talk\
**Created:** [24 March 2006 12:38 UTC](https://rubytalk.org/t/iterator-class-not-working/26071 "2006-03-24T12:38:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pete1](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@Pete1](https://rubytalk.org/u/Pete1)\
**Post date:** [24 March 2006 12:38 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/1 "2006-03-24T12:38:29Z")

</div>

Hi,

in order to understand ruby better (and for fun reasons of course) I try to  
write an iterator class. this way I want to understand 'binding' and  
'callcc' better.

however, it does not work! ☹

any help will be greatly appreciated!!!

> **···**
>
> ---------------------------------------------
> 
> class Iterator
> 
> &nbsp;&nbsp;def initialize(enum)  
> &nbsp;&nbsp;&nbsp;&nbsp;@context = binding  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def next  
> &nbsp;&nbsp;&nbsp;&nbsp;enum = eval("enum", @context)  
> &nbsp;&nbsp;&nbsp;&nbsp;@context, item = callcc do |cont|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enum.each do |item|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cont.call binding, item  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;item  
> &nbsp;&nbsp;end
> 
> end
> 
> names = %w{mary gordy john jane elwood}
> 
> it = Iterator.new(names)
> 
> 3.times do  
> &nbsp;&nbsp;puts it.next  
> end
> 
> > mary  
> > mary  
> > mary
> 
> Best regards  
> Peter

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [24 March 2006 13:17 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/2 "2006-03-24T13:17:32Z")

</div>

> Hi,
> 
> in order to understand ruby better (and for fun reasons of course) I try  
> to  
> write an iterator class. this way I want to understand 'binding' and  
> 'callcc' better.
> 
> however, it does not work! ☹
> 
> any help will be greatly appreciated!!!
> 
> ---------------------------------------------
> 
> class Iterator
> 
> &nbsp;&nbsp;def initialize(enum)  
> &nbsp;&nbsp;&nbsp;&nbsp;@context = binding  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def next  
> &nbsp;&nbsp;&nbsp;&nbsp;enum = eval("enum", @context)  
> &nbsp;&nbsp;&nbsp;&nbsp;@context, item = callcc do |cont|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enum.each do |item|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cont.call binding, item

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^

> **···**
>
> On 3/24/06, Peter Ertl \<pertl@gmx.org\> wrote:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>  
> ------------------------+  
> you are missing "state" here, each time you call next, you start to iterate  
> over your "enum" and  
> jump out of the iterator at the first iteration, that is giving you "mary"  
> all the time.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;item  
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > names = %w{mary gordy john jane elwood}
> > 
> > it = Iterator.new(names)
> > 
> > 3.times do  
> > &nbsp;&nbsp;puts it.next  
> > end
> > 
> > \> mary  
> > \> mary  
> > \> mary
> > 
> > Best regards  
> > Peter
> 
> Well I see why it does not work, but I fail to see your design behind the  
> thing  
> it could be done like this  
> class Iterator  
> &nbsp;&nbsp;&nbsp;&nbsp;def initialize(\*args); @items=args;@i=-1;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;def next  
> @i+=1; return @items[@i] unless block\_given?; yield @items[@i]; end  
> end  
> but I do not really know what that would be good for.
> 
> Cheers  
> Robert
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford4](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Ross\_Bamford4](https://rubytalk.org/u/Ross_Bamford4)\
**Post date:** [24 March 2006 13:49 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/3 "2006-03-24T13:49:41Z")

</div>

Here is a slightly modified version of your code that doesn't cover  
everything, but should get you going in the right direction.

##### code #####  
class Iterator  
&nbsp;&nbsp;def initialize(enum)  
&nbsp;&nbsp;&nbsp;&nbsp;@yield = lambda do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enum.each do |item|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@yield = callcc { |cc|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@next.call cc, item  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "Exhausted"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;def next  
&nbsp;&nbsp;&nbsp;&nbsp;@yield, item = callcc do |cc|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@next = cc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@yield.call  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;item  
&nbsp;&nbsp;end  
end

names = %w{mary gordy john jane elwood}

it = Iterator.new(names)

6.times do  
&nbsp;&nbsp;&nbsp;&nbsp;puts it.next  
end

##### outputs #####

mary  
gordy  
john  
jane  
elwood  
-:9:in `initialize': Exhausted (RuntimeError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from -:16:in `next'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from -:14:in `next'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from -:27  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from -:26

All you need to bear in mind when using continuations this way is that  
what you're effectively needing is to 'emulate' two call stacks on a  
single thread (maybe your 'next' stack and your 'yield' stack) and to  
chop between them by calling the appropriate continuation.

In some respects it's similar to having two threads in a synchronized  
stop/start setup, which is another way you can implement the above. In  
fact...

&nbsp;&nbsp;[Ruby Quiz - FasterGenerator (#66)](http://www.rubyquiz.com/quiz66.html)

I'll leave the details (proper end handling and so on) to you. Btw, I  
know this doesn't use binding but I found that to be a fairly  
unintuitive way to do it. The binding use in your original code was fine  
in itself I think, but you can't use it to retain state the way I think  
you were hoping for.

> **···**
>
> On Fri, 2006-03-24 at 21:38 +0900, Peter Ertl wrote:
> 
> > Hi,
> > 
> > in order to understand ruby better (and for fun reasons of course) I try to  
> > write an iterator class. this way I want to understand 'binding' and  
> > 'callcc' better.
> 
> --  
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk

---

<div class="post-metadata">

**Author:** ![Pete1](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@Pete1](https://rubytalk.org/u/Pete1)\
**Post date:** [24 March 2006 13:27 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/4 "2006-03-24T13:27:58Z")

</div>

Hello Robert,

thanks for your feedback.

Yield'ing and using Array. seems obvious but I want class 'Iterator' to  
work for any 'Enumerable', not only 'Array'.

The intention for using 'binding' is that I expect / hope it will keep track  
of the enumeration state inside 'Enumeration.each'. However, I don't know if  
that's possible in ruby at all.

> **···**
>
> > --- Ursprüngliche Nachricht ---  
> > Von: "Robert Dober" \<robert.dober@gmail.com\>  
> > An: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > Betreff: Re: iterator class not working  
> > Datum: Fri, 24 Mar 2006 22:17:32 +0900
> > 
> > On 3/24/06, Peter Ertl \<pertl@gmx.org\> wrote:  
> > \>  
> > \> Hi,  
> > \>  
> > \> in order to understand ruby better (and for fun reasons of course) I try  
> > \> to  
> > \> write an iterator class. this way I want to understand 'binding' and  
> > \> 'callcc' better.  
> > \>  
> > \> however, it does not work! ☹  
> > \>  
> > \> any help will be greatly appreciated!!!  
> > \>  
> > \> ---------------------------------------------  
> > \>  
> > \> class Iterator  
> > \>  
> > \> def initialize(enum)  
> > \> @context = binding  
> > \> end  
> > \>  
> > \> def next  
> > \> enum = eval("enum", @context)  
> > \> @context, item = callcc do |cont|  
> > \> enum.each do |item|  
> > \> cont.call binding, item
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>  
> > ------------------------+  
> > you are missing "state" here, each time you call next, you start to  
> > iterate  
> > over your "enum" and  
> > jump out of the iterator at the first iteration, that is giving you "mary"  
> > all the time.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > \> end  
> > \> item  
> > \> end  
> > \>  
> > \> end  
> > \>  
> > \> names = %w{mary gordy john jane elwood}  
> > \>  
> > \> it = Iterator.new(names)  
> > \>  
> > \> 3.times do  
> > \> puts it.next  
> > \> end  
> > \>  
> > \> \> mary  
> > \> \> mary  
> > \> \> mary  
> > \>  
> > \> Best regards  
> > \> Peter  
> > \>  
> > \>  
> > Well I see why it does not work, but I fail to see your design behind the  
> > thing  
> > it could be done like this  
> > class Iterator  
> > &nbsp;&nbsp;&nbsp;&nbsp;def initialize(\*args); @items=args;@i=-1;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def next  
> > @i+=1; return @items[@i] unless block\_given?; yield @items[@i]; end  
> > end  
> > but I do not really know what that would be good for.
> > 
> > Cheers  
> > Robert
> > 
> > --  
> > Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> > concerne l'univers, je n'en ai pas acquis la certitude absolue.
> > 
> > - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford4](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Ross\_Bamford4](https://rubytalk.org/u/Ross_Bamford4)\
**Post date:** [24 March 2006 14:10 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/5 "2006-03-24T14:10:05Z")

</div>

Just a small fix:

> &nbsp;&nbsp;def initialize(enum)  
> &nbsp;&nbsp;&nbsp;&nbsp;@yield = lambda do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enum.each do |item|  
> - @yield = callcc { |cc|

&nbsp;&nbsp;+ callcc { |cc|

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@next.call cc, item  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "Exhausted"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end

Doesn't really hurt (as currently written) but it's unnecessary and  
could cause confusion.

> **···**
>
> On Fri, 2006-03-24 at 22:49 +0900, Ross Bamford wrote:
> 
> --  
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [24 March 2006 13:38 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/6 "2006-03-24T13:38:55Z")

</div>

Peter Ertl schrieb:

> Yield'ing and using Array. seems obvious but I want class 'Iterator' to  
> work for any 'Enumerable', not only 'Array'.
> 
> The intention for using 'binding' is that I expect / hope it will keep track  
> of the enumeration state inside 'Enumeration.each'. However, I don't know if  
> that's possible in ruby at all.

Peter, I think you can't use bindings for that. All the continuations based iterator implementations I've seen use at least two continuations. You can find them in the mailing list archives. Of course that's much less fun than implementing it for yourself.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [24 March 2006 13:45 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/7 "2006-03-24T13:45:41Z")

</div>

and you want lazy evaluation, converting the Enumeration into an array is  
not an option, right?  
I think what we need here are coroutines and we do not have them (yet).  
I fail to see a solution, hopefully somebody brighter will enlighten us 😉

Cheers  
Robert

> **···**
>
> On 3/24/06, Peter Ertl \<pertl@gmx.org\> wrote:
> 
> > Hello Robert,
> > 
> > thanks for your feedback.
> > 
> > Yield'ing and using Array. seems obvious but I want class 'Iterator' to  
> > work for any 'Enumerable', not only 'Array'.
> > 
> > The intention for using 'binding' is that I expect / hope it will keep  
> > track  
> > of the enumeration state inside 'Enumeration.each'. However, I don't know  
> > if  
> > that's possible in ruby at all.
> > 
> > \> --- Ursprüngliche Nachricht ---  
> > \> Von: "Robert Dober" \<robert.dober@gmail.com\>  
> > \> An: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > \> Betreff: Re: iterator class not working  
> > \> Datum: Fri, 24 Mar 2006 22:17:32 +0900  
> > \>  
> > \> On 3/24/06, Peter Ertl \<pertl@gmx.org\> wrote:  
> > \> \>  
> > \> \> Hi,  
> > \> \>  
> > \> \> in order to understand ruby better (and for fun reasons of course) I  
> > try  
> > \> \> to  
> > \> \> write an iterator class. this way I want to understand 'binding' and  
> > \> \> 'callcc' better.  
> > \> \>  
> > \> \> however, it does not work! ☹  
> > \> \>  
> > \> \> any help will be greatly appreciated!!!  
> > \> \>  
> > \> \> ---------------------------------------------  
> > \> \>  
> > \> \> class Iterator  
> > \> \>  
> > \> \> def initialize(enum)  
> > \> \> @context = binding  
> > \> \> end  
> > \> \>  
> > \> \> def next  
> > \> \> enum = eval("enum", @context)  
> > \> \> @context, item = callcc do |cont|  
> > \> \> enum.each do |item|  
> > \> \> cont.call binding, item  
> > \>  
> > \>  
> > \> ^  
> > \> \>  
> > \> ------------------------+  
> > \> you are missing "state" here, each time you call next, you start to  
> > \> iterate  
> > \> over your "enum" and  
> > \> jump out of the iterator at the first iteration, that is giving you  
> > "mary"  
> > \> all the time.  
> > \>  
> > \>  
> > \> end  
> > \> \> end  
> > \> \> item  
> > \> \> end  
> > \> \>  
> > \> \> end  
> > \> \>  
> > \> \> names = %w{mary gordy john jane elwood}  
> > \> \>  
> > \> \> it = Iterator.new(names)  
> > \> \>  
> > \> \> 3.times do  
> > \> \> puts it.next  
> > \> \> end  
> > \> \>  
> > \> \> \> mary  
> > \> \> \> mary  
> > \> \> \> mary  
> > \> \>  
> > \> \> Best regards  
> > \> \> Peter  
> > \> \>  
> > \> \>  
> > \> Well I see why it does not work, but I fail to see your design behind  
> > the  
> > \> thing  
> > \> it could be done like this  
> > \> class Iterator  
> > \> def initialize(\*args); @items=args;@i=-1;end  
> > \>  
> > \> def next  
> > \> @i+=1; return @items[@i] unless block\_given?; yield @items[@i]; end  
> > \> end  
> > \> but I do not really know what that would be good for.  
> > \>  
> > \> Cheers  
> > \> Robert  
> > \>  
> > \> --  
> > \> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> > \> concerne l'univers, je n'en ai pas acquis la certitude absolue.  
> > \>  
> > \> - Albert Einstein  
> > \>
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Pete1](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@Pete1](https://rubytalk.org/u/Pete1)\
**Post date:** [24 March 2006 14:25 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/8 "2006-03-24T14:25:59Z")

</div>

Thanks for all you help.

Finally, using two nested continuations works well...

Here's my working code ... I am very curious about  
optimization and alternatives from you guts. Afterall  
I found this issue a very exciting one!!

class Iterator

&nbsp;&nbsp;def initialize(obj, method = :each)  
&nbsp;&nbsp;&nbsp;&nbsp;@obj = obj  
&nbsp;&nbsp;&nbsp;&nbsp;@method = method  
&nbsp;&nbsp;end

&nbsp;&nbsp;def next  
&nbsp;&nbsp;&nbsp;&nbsp;raise "done" if @done  
&nbsp;&nbsp;&nbsp;&nbsp;@iteration.call if @iteration  
&nbsp;&nbsp;&nbsp;&nbsp;@iteration, @current = callcc do |loop|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@obj.send(@method) do |item|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;callcc do |state|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop.call state, item  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nil  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;@current  
&nbsp;&nbsp;end

end

names = %w{peter paul mary gordy john jane elwood}

it = Iterator.new(names)  
while item = it.next  
&nbsp;&nbsp;puts item  
end

it = Iterator.new("Teststring", :each\_byte)  
while item = it.next  
&nbsp;&nbsp;puts item.chr  
end

peter  
paul  
mary  
gordy  
john  
jane  
elwood  
T  
e  
s  
t  
s  
t  
r  
i  
n  
g

> **···**
>
> > --- Ursprüngliche Nachricht ---  
> > Von: Ross Bamford \<rossrt@roscopeco.co.uk\>  
> > An: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > Betreff: Re: iterator class not working  
> > Datum: Fri, 24 Mar 2006 23:10:05 +0900
> > 
> > Just a small fix:
> > 
> > On Fri, 2006-03-24 at 22:49 +0900, Ross Bamford wrote:  
> > \> def initialize(enum)  
> > \> @yield = lambda do  
> > \> enum.each do |item|  
> > \> - @yield = callcc { |cc|  
> > &nbsp;&nbsp;+ callcc { |cc|  
> > \> @next.call cc, item  
> > \> }  
> > \> end  
> > \> raise "Exhausted"  
> > \> end  
> > \> end
> > 
> > Doesn't really hurt (as currently written) but it's unnecessary and  
> > could cause confusion.
> > 
> > --  
> > Ross Bamford - rosco@roscopeco.REMOVE.co.uk

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [24 March 2006 14:43 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/9 "2006-03-24T14:43:15Z")

</div>

> Just a small fix:
> 
> \> def initialize(enum)  
> \> @yield = lambda do  
> \> enum.each do |item|  
> \> - @yield = callcc { |cc|  
> &nbsp;&nbsp;+ callcc { |cc|  
> \> @next.call cc, item  
> \> }  
> \> end  
> \> raise "Exhausted"  
> \> end  
> \> end
> 
> Doesn't really hurt (as currently written) but it's unnecessary and  
> could cause confusion.
> 
> --  
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk

Well I got it!  
Was I missing something or is my solution really better?

---------------------------------- 8\< --------------------------  
class Iterator  
&nbsp;&nbsp;&nbsp;&nbsp;class Exhausted \< Exception; end  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize( enum )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@enum = enum  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@next = nil  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def next  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return @next.call if @next  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@enum.each do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>item\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;callcc{ |cc|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@next = cc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return item  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end # do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise Exhausted, "No more items :("  
&nbsp;&nbsp;&nbsp;&nbsp;end # def next  
end # class Iterator

i = Iterator.new( %w{ Ringo John Paul George } )  
loop do; puts i.next; end

> **···**
>
> On 3/24/06, Ross Bamford \<rossrt@roscopeco.co.uk\> wrote:
> 
> > On Fri, 2006-03-24 at 22:49 +0900, Ross Bamford wrote:
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford4](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Ross\_Bamford4](https://rubytalk.org/u/Ross_Bamford4)\
**Post date:** [24 March 2006 13:54 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/10 "2006-03-24T13:54:42Z")

</div>

A while ago, while rather more bored than I'd have liked, I came up with  
a toy coroutine idea that was kind of fun for a bit 🙂

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/180136](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/180136)

> **···**
>
> On Fri, 2006-03-24 at 22:45 +0900, Robert Dober wrote:
> 
> > I think what we need here are coroutines and we do not have them (yet).
> 
> --  
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk

---

<div class="post-metadata">

**Author:** ![Pete1](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@Pete1](https://rubytalk.org/u/Pete1)\
**Post date:** [24 March 2006 14:27 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/11 "2006-03-24T14:27:58Z")

</div>

really UGLY typo!!

don't take it personal 🙂

> optimization and alternatives from you gu\_t\_s. Afterall

guys!!!!!

> **···**
>
> > --- Ursprüngliche Nachricht ---  
> > Von: "Peter Ertl" \<pertl@gmx.org\>  
> > An: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > Betreff: Re: iterator class not working  
> > Datum: Fri, 24 Mar 2006 23:25:59 +0900
> > 
> > Thanks for all you help.
> > 
> > Finally, using two nested continuations works well...
> > 
> > Here's my working code ... I am very curious about  
> > optimization and alternatives from you guts. Afterall  
> > I found this issue a very exciting one!!
> > 
> > class Iterator
> > 
> > &nbsp;&nbsp;def initialize(obj, method = :each)  
> > &nbsp;&nbsp;&nbsp;&nbsp;@obj = obj  
> > &nbsp;&nbsp;&nbsp;&nbsp;@method = method  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def next  
> > &nbsp;&nbsp;&nbsp;&nbsp;raise "done" if @done  
> > &nbsp;&nbsp;&nbsp;&nbsp;@iteration.call if @iteration  
> > &nbsp;&nbsp;&nbsp;&nbsp;@iteration, @current = callcc do |loop|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@obj.send(@method) do |item|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;callcc do |state|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop.call state, item  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nil  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;@current  
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > names = %w{peter paul mary gordy john jane elwood}
> > 
> > it = Iterator.new(names)  
> > while item = it.next  
> > &nbsp;&nbsp;puts item  
> > end
> > 
> > it = Iterator.new("Teststring", :each\_byte)  
> > while item = it.next  
> > &nbsp;&nbsp;puts item.chr  
> > end
> > 
> > peter  
> > paul  
> > mary  
> > gordy  
> > john  
> > jane  
> > elwood  
> > T  
> > e  
> > s  
> > t  
> > s  
> > t  
> > r  
> > i  
> > n  
> > g
> > 
> > \> --- Ursprüngliche Nachricht ---  
> > \> Von: Ross Bamford \<rossrt@roscopeco.co.uk\>  
> > \> An: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > \> Betreff: Re: iterator class not working  
> > \> Datum: Fri, 24 Mar 2006 23:10:05 +0900  
> > \>  
> > \> Just a small fix:  
> > \>  
> > \> On Fri, 2006-03-24 at 22:49 +0900, Ross Bamford wrote:  
> > \> \> def initialize(enum)  
> > \> \> @yield = lambda do  
> > \> \> enum.each do |item|  
> > \> \> - @yield = callcc { |cc|  
> > \> + callcc { |cc|  
> > \> \> @next.call cc, item  
> > \> \> }  
> > \> \> end  
> > \> \> raise "Exhausted"  
> > \> \> end  
> > \> \> end  
> > \>  
> > \> Doesn't really hurt (as currently written) but it's unnecessary and  
> > \> could cause confusion.  
> > \>  
> > \> --  
> > \> Ross Bamford - rosco@roscopeco.REMOVE.co.uk  
> > \>  
> > \>

---

<div class="post-metadata">

**Author:** ![Pete1](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@Pete1](https://rubytalk.org/u/Pete1)\
**Post date:** [24 March 2006 14:28 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/12 "2006-03-24T14:28:52Z")

</div>

aargh, again...

remove that line, please:

> **···**
>
> > raise "done" if @done
> 
> > --- Ursprüngliche Nachricht ---  
> > Von: "Peter Ertl" \<pertl@gmx.org\>  
> > An: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > Betreff: Re: iterator class not working  
> > Datum: Fri, 24 Mar 2006 23:25:59 +0900
> > 
> > Thanks for all you help.
> > 
> > Finally, using two nested continuations works well...
> > 
> > Here's my working code ... I am very curious about  
> > optimization and alternatives from you guts. Afterall  
> > I found this issue a very exciting one!!
> > 
> > class Iterator
> > 
> > &nbsp;&nbsp;def initialize(obj, method = :each)  
> > &nbsp;&nbsp;&nbsp;&nbsp;@obj = obj  
> > &nbsp;&nbsp;&nbsp;&nbsp;@method = method  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def next  
> > &nbsp;&nbsp;&nbsp;&nbsp;raise "done" if @done  
> > &nbsp;&nbsp;&nbsp;&nbsp;@iteration.call if @iteration  
> > &nbsp;&nbsp;&nbsp;&nbsp;@iteration, @current = callcc do |loop|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@obj.send(@method) do |item|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;callcc do |state|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop.call state, item  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nil  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;@current  
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > names = %w{peter paul mary gordy john jane elwood}
> > 
> > it = Iterator.new(names)  
> > while item = it.next  
> > &nbsp;&nbsp;puts item  
> > end
> > 
> > it = Iterator.new("Teststring", :each\_byte)  
> > while item = it.next  
> > &nbsp;&nbsp;puts item.chr  
> > end
> > 
> > peter  
> > paul  
> > mary  
> > gordy  
> > john  
> > jane  
> > elwood  
> > T  
> > e  
> > s  
> > t  
> > s  
> > t  
> > r  
> > i  
> > n  
> > g
> > 
> > \> --- Ursprüngliche Nachricht ---  
> > \> Von: Ross Bamford \<rossrt@roscopeco.co.uk\>  
> > \> An: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > \> Betreff: Re: iterator class not working  
> > \> Datum: Fri, 24 Mar 2006 23:10:05 +0900  
> > \>  
> > \> Just a small fix:  
> > \>  
> > \> On Fri, 2006-03-24 at 22:49 +0900, Ross Bamford wrote:  
> > \> \> def initialize(enum)  
> > \> \> @yield = lambda do  
> > \> \> enum.each do |item|  
> > \> \> - @yield = callcc { |cc|  
> > \> + callcc { |cc|  
> > \> \> @next.call cc, item  
> > \> \> }  
> > \> \> end  
> > \> \> raise "Exhausted"  
> > \> \> end  
> > \> \> end  
> > \>  
> > \> Doesn't really hurt (as currently written) but it's unnecessary and  
> > \> could cause confusion.  
> > \>  
> > \> --  
> > \> Ross Bamford - rosco@roscopeco.REMOVE.co.uk  
> > \>  
> > \>

---

<div class="post-metadata">

**Author:** ![Pete1](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@Pete1](https://rubytalk.org/u/Pete1)\
**Post date:** [24 March 2006 14:52 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/13 "2006-03-24T14:52:37Z")

</div>

Indeed, you topped my solution my needing one callcc less...

I allowed myself to change it a little so it will return  
nil once the enumeration is done and does not need exceptions:

class Iterator  
&nbsp;&nbsp;def initialize( enum )  
&nbsp;&nbsp;&nbsp;&nbsp;@enum = enum  
&nbsp;&nbsp;&nbsp;&nbsp;@next = nil  
&nbsp;&nbsp;end

&nbsp;&nbsp;def next  
&nbsp;&nbsp;&nbsp;&nbsp;@next.call if @next  
&nbsp;&nbsp;&nbsp;&nbsp;@enum.each do |item|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;callcc do |cc|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@next = cc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return item  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;nil  
&nbsp;&nbsp;end  
end

i = Iterator.new( %w{ Ringo John Paul George } )

while item = i.next  
&nbsp;&nbsp;puts item  
end

> **···**
>
> > --- Ursprüngliche Nachricht ---  
> > Von: "Robert Dober" \<robert.dober@gmail.com\>  
> > An: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > Betreff: Re: iterator class not working  
> > Datum: Fri, 24 Mar 2006 23:43:15 +0900
> > 
> > On 3/24/06, Ross Bamford \<rossrt@roscopeco.co.uk\> wrote:  
> > \>  
> > \> Just a small fix:  
> > \>  
> > \> On Fri, 2006-03-24 at 22:49 +0900, Ross Bamford wrote:  
> > \> \> def initialize(enum)  
> > \> \> @yield = lambda do  
> > \> \> enum.each do |item|  
> > \> \> - @yield = callcc { |cc|  
> > \> + callcc { |cc|  
> > \> \> @next.call cc, item  
> > \> \> }  
> > \> \> end  
> > \> \> raise "Exhausted"  
> > \> \> end  
> > \> \> end  
> > \>  
> > \> Doesn't really hurt (as currently written) but it's unnecessary and  
> > \> could cause confusion.  
> > \>  
> > \> --  
> > \> Ross Bamford - rosco@roscopeco.REMOVE.co.uk
> > 
> > Well I got it!  
> > Was I missing something or is my solution really better?
> > 
> > ---------------------------------- 8\< --------------------------  
> > class Iterator  
> > &nbsp;&nbsp;&nbsp;&nbsp;class Exhausted \< Exception; end  
> > &nbsp;&nbsp;&nbsp;&nbsp;def initialize( enum )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@enum = enum  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@next = nil  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def next  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return @next.call if @next  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@enum.each do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>item\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;callcc{ |cc|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@next = cc  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return item  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end # do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise Exhausted, "No more items :("  
> > &nbsp;&nbsp;&nbsp;&nbsp;end # def next  
> > end # class Iterator
> > 
> > i = Iterator.new( %w{ Ringo John Paul George } )  
> > loop do; puts i.next; end
> > 
> > --  
> > Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> > concerne l'univers, je n'en ai pas acquis la certitude absolue.
> > 
> > - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford4](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Ross\_Bamford4](https://rubytalk.org/u/Ross_Bamford4)\
**Post date:** [24 March 2006 15:07 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/14 "2006-03-24T15:07:01Z")

</div>

Well, if you're going to cheat and use return ... 🙂

Seriously though, I like that one. Shows how it's important to remember  
the 'normal' stuff when you're thinking in this high level stuff 🙂

And yes, I guess it is better:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
Iterator1 4.730000 0.040000 4.770000 ( 4.877037)  
Iterator2 2.840000 0.050000 2.890000 ( 2.960747)  
Iterator3 2.800000 0.130000 2.930000 ( 2.998121)  
Iterator4 2.040000 0.020000 2.060000 ( 2.131763)

(1 = my updated, easy to follow version of Peter's original, 2 = same  
version with one callcc and one catch, 3 = Peter's second  
implementation, 4 = your callcc/return implementation)

As you can see, though, they're all pretty slow when it comes right down  
to it (this was only over (0...50000).to\_a) - some of the  
FasterGenerator quiz entries I mentioned achieved quite amazing speeds  
over that many iterations and more - you should check them out.

> **···**
>
> On Fri, 2006-03-24 at 23:43 +0900, Robert Dober wrote:
> 
> > Well I got it!  
> > Was I missing something or is my solution really better?
> 
> --  
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [24 March 2006 14:05 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/15 "2006-03-24T14:05:00Z")

</div>

Well it really will take me hours to figure your magic out, but looks like  
coroutines to me.  
This is really impressive.  
Cheers

> **···**
>
> On 3/24/06, Ross Bamford \<rossrt@roscopeco.co.uk\> wrote:
> 
> > On Fri, 2006-03-24 at 22:45 +0900, Robert Dober wrote:
> > 
> > \> I think what we need here are coroutines and we do not have them (yet).
> > 
> > A while ago, while rather more bored than I'd have liked, I came up with  
> > a toy coroutine idea that was kind of fun for a bit 🙂
> > 
> > [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/180136](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/180136)
> > 
> > --  
> > Ross Bamford - rosco@roscopeco.REMOVE.co.uk
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [24 March 2006 15:23 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/16 "2006-03-24T15:23:40Z")

</div>

your continuation stuff, when I realized what callcc really does, I realized  
that I was wrong about the coroutines.  
Ty for the acknowledgement but I learnt it from your code!  
Cheers

> **···**
>
> On 3/24/06, Ross Bamford \<rossrt@roscopeco.co.uk\> wrote:
> 
> > On Fri, 2006-03-24 at 23:43 +0900, Robert Dober wrote:  
> > \>  
> > \> Well I got it!  
> > \> Was I missing something or is my solution really better?
> > 
> > Well, if you're going to cheat and use return ... 🙂
> > 
> > Seriously though, I like that one. Shows how it's important to remember  
> > the 'normal' stuff when you're thinking in this high level stuff 🙂
> > 
> > And yes, I guess it is better:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> > Iterator1 4.730000 0.040000 4.770000 ( 4.877037)  
> > Iterator2 2.840000 0.050000 2.890000 ( 2.960747)  
> > Iterator3 2.800000 0.130000 2.930000 ( 2.998121)  
> > Iterator4 2.040000 0.020000 2.060000 ( 2.131763)
> > 
> > (1 = my updated, easy to follow version of Peter's original, 2 = same  
> > version with one callcc and one catch, 3 = Peter's second  
> > implementation, 4 = your callcc/return implementation)
> > 
> > As you can see, though, they're all pretty slow when it comes right down  
> > to it (this was only over (0...50000).to\_a) - some of the  
> > FasterGenerator quiz entries I mentioned achieved quite amazing speeds  
> > over that many iterations and more - you should check them out.
> > 
> > --  
> > Ross Bamford - rosco@roscopeco.REMOVE.co.uk
> > 
> > I have no merit than ;), no it is true that I have not understood a bit of
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [24 March 2006 16:14 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/17 "2006-03-24T16:14:09Z")

</div>

Peter Ertl schrieb:

> class Iterator  
> &nbsp;&nbsp;...  
> end
> 
> i = Iterator.new( %w{ Ringo John Paul George } )
> 
> while item = i.next  
> &nbsp;&nbsp;puts item  
> end

Peter, test your code with

&nbsp;&nbsp;&nbsp;puts i.next

I don't think you can get away with only one continuation.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Nate\_Smith](https://avatars.discourse-cdn.com/v4/letter/n/ecae2f/32.png) [@Nate\_Smith](https://rubytalk.org/u/Nate_Smith)\
**Post date:** [24 March 2006 16:22 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/18 "2006-03-24T16:22:02Z")

</div>

Hello,

What is the benefit in ruby of allowing the following syntax:

in irb:

irb(main):005:0\> 1 + + + + 3  
=\> 4  
irb(main):006:0\> 1 + - + 3  
=\> -2  
irb(main):007:0\> 1 - - - - 3  
=\> 4  
irb(main):008:0\> 1 - + - 3  
=\> 4  
irb(main):010:0\> 1 - +-3  
=\> 4

Is this a feature or a gotcha?

Nate

---

<div class="post-metadata">

**Author:** ![Pete1](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@Pete1](https://rubytalk.org/u/Pete1)\
**Post date:** [24 March 2006 16:33 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/19 "2006-03-24T16:33:50Z")

</div>

whoa, amazing + surprising!!!

now, I have to check that code again ... 🙂

> **···**
>
> > --- Ursprüngliche Nachricht ---  
> > Von: Pit Capitain \<pit@capitain.de\>  
> > An: ruby-talk@ruby-lang.org (ruby-talk ML)  
> > Betreff: Re: iterator class not working  
> > Datum: Sat, 25 Mar 2006 01:14:09 +0900
> > 
> > Peter Ertl schrieb:  
> > \> class Iterator  
> > \> ...  
> > \> end  
> > \>  
> > \> i = Iterator.new( %w{ Ringo John Paul George } )  
> > \>  
> > \> while item = i.next  
> > \> puts item  
> > \> end
> > 
> > Peter, test your code with
> > 
> > &nbsp;&nbsp;&nbsp;puts i.next  
> > &nbsp;&nbsp;&nbsp;puts i.next
> > 
> > I don't think you can get away with only one continuation.
> > 
> > Regards,  
> > Pit

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [24 March 2006 16:32 UTC](https://rubytalk.org/t/iterator-class-not-working/26071/20 "2006-03-24T16:32:53Z")

</div>

you are just saying

&nbsp;&nbsp;&nbsp;1 + (+(+(+3)))

in otherwords you have made 3 \_\_very\_\_ positive. if you could not do that  
you'd not be able to do this:

&nbsp;&nbsp;&nbsp;jib:~ \> ruby -e' p 41 - -1 '  
&nbsp;&nbsp;&nbsp;42

C is going to allow this too:

&nbsp;&nbsp;&nbsp;jib:~ \> cat a.c && gcc a.c && a.out  
&nbsp;&nbsp;&nbsp;main(){ printf ("%d\n", 41 + + + 1); }  
&nbsp;&nbsp;&nbsp;42

it's just a result of binding/associvity.

regards.

-a

> **···**
>
> On Sat, 25 Mar 2006, Nate Smith wrote:
> 
> > Hello,
> > 
> > What is the benefit in ruby of allowing the following syntax:
> > 
> > in irb:
> > 
> > irb(main):005:0\> 1 + + + + 3  
> > =\> 4
> 
> --  
> share your knowledge. it's a way to achieve immortality.  
> - h.h. the 14th dali lama

[Next page](https://rubytalk.org/t/iterator-class-not-working/26071.md?page=2)
