------art_7089_10754559.1143206235805
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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óŐise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

------art_7089_10754559.1143206235805--