On Tue, Mar 25, 2003 at 10:03:09PM +0900, Han Holl wrote:
> Julian Snitow <vangczung / yahoo.com> wrote in message 
> [ cut ]
> > 
> > Also, it just occured to me that it might be more Rubylike to have a 
> > mixin method like
> > 
> > module Iteration
> >    class ExternalIterator
> >       # class definition same as above
> >    end
> > 
> >    def iter
> > 	ExternalIterator.new(self)
> >    end
> > end
> > 
> > Then you'd just do something like
> > 
> > class Array
> >    include Iteration
> > end
> > 
> > ex = ["foo", "bar", "baz", "qux", "florp", "bzaa", "worble"].iter
> > 
> > # then call ex.get whenever you want the next value
> > ###################################
> 
> It may be more Rubyesque, but unfortunately it doesn't work:

The problem is not related to the use of Module#include, and it can be fixed:

batsman@tux-chan:/tmp$ expand -t 2 s.rb
class ExternalIterator
  def initialize(collection)
    @collection = collection
    @cont = nil
    @outer = nil
  end
  def get
    callcc do |@outer|
      @cont.call if @cont
      @collection.each { |elem|
        callcc { |@cont|
          @outer.call elem
        }
      }
      @cont = nil  # reset the continuation and return nil
    end
  end
end

module Iteration
  ExternalIterator = ::ExternalIterator

  def iter
    ExternalIterator.new(self)
  end
end

class Array
   include Iteration
end

arr = ["foo", "bar", "baz", "qux", "florp", "bzaa", "worble"]
ex = arr.iter

puts ex.get
puts "---"
puts ex.get
puts "***"

puts "=" * 80
ex = ExternalIterator.new(arr)

puts ex.get
puts "---"
puts ex.get
puts "***"
batsman@tux-chan:/tmp$ ruby s.rb
foo
---
bar
***
================================================================================
foo
---
bar
***


Don't mix that with Thread :)

-- 
 _           _                             
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ 
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
	Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Save yourself from the 'Gates' of hell, use Linux."  -- like that one.
	-- The_Kind @ LinuxNet