On 24 Aug 2001, at 22:35, Paul Brannan wrote:

> (... arguments to support ++ operator skipped ...)

> This is all fine and dandy, but consider the case where f and b are
> not indexable like this.  Iterating through them is trivial in C++
> (note that I've sacrificed some correctness for readability here):
> 
>     Type::const_iterator f_itor = f.begin();
>     Type::const_iterator b_itor = b.begin();
>     for(; f != f.end() && b != b.end(); ++f, ++b)
>     {
>       cout << "(" << *f << ", " << *b << ")" << endl;
>     }
> 
> However, in Ruby, aside from using weird callcc hacks, the best I can
> hope for is to use collect or to_a or something to convert the data
> into an array so that I can iterate through it.
> 
> Granted, this is not a very common case (I've actually never needed to
> do it in Ruby), but it is a limitation of Ruby iterators.

Hi Paul,

if you hide the "weird callcc hacks" in a class like the Iterator class 
I posted today in [ruby-talk:20270], your example could be written 
in Ruby as

f_itor = f.iterator
b_itor = b.iterator
while f_itor.has_next && b_itor.has_next
  puts "(#{f_itor.next}, #{b_itor.next})"
end


You have to extend module Enumerable like this:

module Enumerable
  def iterator
    Iterator.new( self )
  end
end


Pit