[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: iterator class not working

Robert Dober

3/24/2006 1:18:00 PM

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
3 Answers

Pete

3/24/2006 1:28:00 PM

0

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
>


Ross Bamford

3/24/2006 1:55:00 PM

0

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-t...

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk



Robert Dober

3/24/2006 2:05:00 PM

0

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-t...
>
> --
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk
>
>
>
Well it really will take me hours to figure your magic out, but looks like
coroutines to me.
This is really impressive.
Cheers

--
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