On Thu, 10 Mar 2005 21:52:43 +0900, Roshan James <roshanj / microsoft.com> wrote:
> I have been wondering is there is a way to do this. Can I have two
> itertors working in lock-step - ie, can I have both of them return a
> value each into the same loop body ?
> 
> a = [1, 2, 3, 4]
> b = [5, 6, 7, 8]
> a.each {|a1|
>     b.each {|b1|
>         # this nests the call to b inside a
>     }
> }
> 
> Is there someway I can do something like -
> a = [1, 2, 3, 4]
> b = [5, 6, 7, 8]
> (a.each, b.each) {|a1, b1|
>         #so that I get one value from a and one from b
> }
> 
> I hope I have made the idea clear, the above syntax is only illustrative
> of what I mean.
> 
> This is possible in python because the iterators are fundamentally
> objects that require an explicit next() call and are not bound to the
> the loop body/block by syntax as in ruby.
> 
> Thanks in advance,
> Roshan
> 

Hello Roshan,

you can use the Generator class, to wrap any iterator into a generator
(i.e. get a .next function)

Or in simple cases where the additional memory overhead is not
important, you can also use the zip funtion. I.e.

%w(eins zwei drei).zip([1,2,3]).each do | name, value |
  puts "The value of #{name} is #{value}"
end

regards,

Brian

-- 
Brian Schröäer
http://ruby.brian-schroeder.de/