On Nov 28, 7:29 am, "Just Another Victim of the Ambient Morality"
<ihates... / hotmail.com> wrote:
>     One weakness of Ruby iterators that continuations don't have is parallel
> iteration.  If you have two containers that represent different aspects of
> the same things, it's difficult to iterate over both of them in Ruby.  In
> Python, you can do this:
>
> list1 = [1, 2, 3, 4]
> list2 = ['a', 'b', 'c', 'd']
>
> # Okay, Python is more wordy than I'd like
> # I could have just used zip() but you rarely need the list!
> for num, letter in itertools.izip(list1, list2):
>     # Do something with both numbers and letters
>     print num, letter
>     print
>
>     How would one do this in Ruby?

At RubyConf 2007 last month, Matz announced that 1.9 would have
external iterators.  That would allow you to do this:

num_iter = list1.each
let_iter = list2.each
loop {
  puts num_iter.next, let_iter.next
}

When you reach the end of either list (or any list, if we scale beyond
just two), the call to next would raise an exception, automatically
caught by the loop construct, causing the loop to simply exit.  So
this construct works gracefully even if one of the lists is shorter
than the other(s).

Eric

P.S. I put together a very short presentation for the Southeast
Michigan Ruby Users' Group summarizing what Matz said about 1.9.  You
can see the slides at:

    http://learnruby.com/ruby-1.9.html

====

Are you interested in on-site Ruby training that uses well-designed,
real-world, hands-on exercises? http://LearnRuby.com