# going over two interators - Ruby

I often find that I need to go over two enumerables in parallel. I have used a little method called 'both' for this: irb arr1 = [1,2,3,4,5] --> [1, 2, 3, 4, 5] arr2 = [6,7,8,9,10] --> [6, 7, 8, 9, 10] def both( enum1, enum2 ) enumC = enum2.dup enum1.each do |e1| yield e1, enumC.shift end end --> nil both( arr1, arr2 ) do |a,b| puts "#{a} : #{b}" end 1 : 6 2 : 7 3 : 8 4 : 9 5 : 10 --> [1, 2, 3, 4, 5] It assumes that enum1 is at least at ...

1. ## going over two interators

I often find that I need to go over two enumerables in parallel. I have
used a little method called 'both' for this:

irb
arr1 = [1,2,3,4,5]
--> [1, 2, 3, 4, 5]
arr2 = [6,7,8,9,10]
--> [6, 7, 8, 9, 10]
def both( enum1, enum2 )
enumC = enum2.dup
enum1.each do |e1|
yield e1, enumC.shift
end
end
--> nil
both( arr1, arr2 ) do |a,b|
puts "#{a} : #{b}"
end
1 : 6
2 : 7
3 : 8
4 : 9
5 : 10
--> [1, 2, 3, 4, 5]

It assumes that enum1 is at least at big as enum2.

Is there a way someone can think of that's more efficient then this?
Namely getting rid of the 'dup'. Or maybe I'm missing some rubyism that
would make this easier.

TIA,
Michael

Michael Garriss Guest

2. ## Re: going over two interators

Michael Garriss wrote:
> I often find that I need to go over two enumerables in parallel. I
> have used a little method called 'both' for this:
>
> irb
> arr1 = [1,2,3,4,5]
> --> [1, 2, 3, 4, 5]
> arr2 = [6,7,8,9,10]
> --> [6, 7, 8, 9, 10]
> def both( enum1, enum2 )
> enumC = enum2.dup
> enum1.each do |e1|
> yield e1, enumC.shift
> end
> end
> --> nil
> both( arr1, arr2 ) do |a,b|
> puts "#{a} : #{b}"
> end
> 1 : 6
> 2 : 7
> 3 : 8
> 4 : 9
> 5 : 10
> --> [1, 2, 3, 4, 5]
>
>
> It assumes that enum1 is at least at big as enum2.
>
> Is there a way someone can think of that's more efficient then this?
> Namely getting rid of the 'dup'. Or maybe I'm missing some rubyism
> that would make this easier.
>
I just realized that a) my spell checker doesn't look at the subject
line, and that b) this only works for arrays. How could I make it work
(well) for any enumerable?

Michael

Michael Garriss Guest

3. ## Re: going over two interators

On Thu, 18 Sep 2003 08:27:08 +0900, Michael Garriss wrote:

In Ruby 1.8, try Enumerable#zip.

-austin
--
austin ziegler * [email]austinhalostatue.ca[/email] * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.09.17
* 21.58.04

Austin Ziegler Guest

4. ## Re: going over two interators

Austin Ziegler wrote:
>In Ruby 1.8, try Enumerable#zip.
>
>
That's the one. Thanks to you and the lucky stiff....

Michael

Michael Garriss Guest

5. ## Re: going over two interators

On Thu, Sep 18, 2003 at 08:23:59AM +0900, Michael Garriss wrote:
> I often find that I need to go over two enumerables in parallel.
Have a look at these:

[url]http://www.rubycentral.com/faq/rubyfaq-5.html#ss5.5[/url]
[url]http://www.rubygarden.org/article.php?sid=107[/url]
[url]http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/rubycollections/rubycollections/rbc/iterator/[/url]
[url]http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/rough/lib/generator.rb?rev=1.6&content-type=text/x-cvsweb-markup[/url]
[url]http://rgl.sourceforge.net/files/stream_rb.html[/url]

Paul

Paul Brannan Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•