From: Peter Ertl Date: 2006-03-24T23:52:37+09:00 Subject: Re: iterator class not working 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 def initialize( enum ) @enum = enum @next = nil end def next @next.call if @next @enum.each do |item| callcc do |cc| @next = cc return item end end nil end end i = Iterator.new( %w{ Ringo John Paul George } ) while item = i.next puts item end > --- Urspr�ngliche Nachricht --- > Von: "Robert Dober" > 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 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 > class Exhausted < Exception; end > def initialize( enum ) > @enum = enum > @next = nil > end > > def next > return @next.call if @next > @enum.each do > |item| > callcc{ |cc| > @next = cc > return item > } > end # do > raise Exhausted, "No more items :(" > 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 >