# An idiomatic way to traverse all pairs in an enumerable?

**URL:** <https://www.ruby-forum.com/t/an-idiomatic-way-to-traverse-all-pairs-in-an-enumerable/123288>\
**Category:** Ruby\
**Created:** [December 16, 2007, 12:04am UTC](https://www.ruby-forum.com/t/an-idiomatic-way-to-traverse-all-pairs-in-an-enumerable/123288 "2007-12-16T00:04:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [December 16, 2007, 12:04am UTC](https://www.ruby-forum.com/t/an-idiomatic-way-to-traverse-all-pairs-in-an-enumerable/123288/1 "2007-12-16T00:04:42Z")

</div>

I seem to end up doing the following quite a lot:

enum.each do |a|  
enum.each do |b|  
next if a \>= b  
# do\_something with a and b  
end  
end

In general, Iâ€™m looking for an idiomatic way of traversing each  
possible pair of a given enumâ€™s contents (once). Is there a more  
compact construct?

(I assume I could finally grasp the whole block-passing side of Ruby  
and write my own Enumerable#each\_mix, but maybe thereâ€™s something there  
already. Note: Enumerable#each\_pair doesnâ€™t cut it, as itâ€™s just for  
neighbouring element pairs, while I need something for all possible  
pairs.)

Thanks in advance for any help with the above!

– Shot

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [December 16, 2007, 1:27am UTC](https://www.ruby-forum.com/t/an-idiomatic-way-to-traverse-all-pairs-in-an-enumerable/123288/2 "2007-12-16T01:27:45Z")

</div>

On Dec 15, 2007, at 6:03 PM, Shot (Piotr S.) wrote:

> possible pair of a given enum’s contents (once). Is there a more
> 
> ## – Shot
> 
> The existential root of libertarianism is the experience of  
> being very bad at taking orders from morons. – Leopold Leider

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/42671](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/42671)

You could use the Array#combination code posted there either directly  
or as an example. If you can rely on the ordering of elements (your  
a\>=b), your code isn’t too bad, but the code on the ruby list will be  
(is) part of Ruby1.9 and only depends on position within the Array.  
If you have an enumerable that’s not an Array, you might have some  
tweaking to do anyway.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [December 16, 2007, 11:05am UTC](https://www.ruby-forum.com/t/an-idiomatic-way-to-traverse-all-pairs-in-an-enumerable/123288/3 "2007-12-16T11:05:52Z")

</div>

On Dec 15, 6:25 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > end  
> > neighbouring element pairs, while I need something for all possible
> 
> removed\_email\_address@domain.invalid  
> module Enumerable  
> def every\_pair(other)  
> self.each { | x |  
> other.each { | y |  
> yield [x, y] if x \< y  
> }  
> }  
> end  
> end

a = [1, 8, 3]  
b = [4, 5, 6]  
a.every\_pair(b) { | x, y |  
puts "doing stuff with " + [x, y].inspect  
}

# =\>

doing stuff with [1, 4]  
doing stuff with [1, 5]  
doing stuff with [1, 6]  
doing stuff with [3, 4]  
doing stuff with [3, 5]  
doing stuff with [3, 6]

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [December 16, 2007, 4:04pm UTC](https://www.ruby-forum.com/t/an-idiomatic-way-to-traverse-all-pairs-in-an-enumerable/123288/4 "2007-12-16T16:04:04Z")

</div>

MonkeeSage:

> module Enumerable  
> def every\_pair(other)  
> self.each { | x |  
> other.each { | y |  
> yield [x, y] if x \< y  
> }  
> }  
> end  
> end

Ah, perfect. In my (limited) case, it ended up to be

module Enumerable  
def every\_pair  
each\_with\_index do |a, i|  
each\_with\_index do |b, j|  
yield [a, b] if i \< j  
end  
end  
end  
end

> > arr = (1…5).sort\_by { rand }  
> > =\> [1, 4, 2, 5, 3]
> 
> > arr.every\_pair { |a, b| p [a, b] }  
> > [1, 4]  
> > [1, 2]  
> > [1, 5]  
> > [1, 3]  
> > [4, 2]  
> > [4, 5]  
> > [4, 3]  
> > [2, 5]  
> > [2, 3]  
> > [5, 3]  
> > =\> [1, 4, 2, 5, 3]

Most appreciated!

– Shot

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [December 16, 2007, 3:23pm UTC](https://www.ruby-forum.com/t/an-idiomatic-way-to-traverse-all-pairs-in-an-enumerable/123288/5 "2007-12-16T15:23:04Z")

</div>

On Dec 15, 6:03 pm, “Shot (Piotr S.)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> possible pair of a given enum’s contents (once). Is there a more  
> compact construct?

Facets exits for you.

[http://facets.rubyforge.org/rdoc/core/classes/Enumerable.html#M000450](http://facets.rubyforge.org/rdoc/core/classes/Enumerable.html#M000450)

T.

[http://facets.rubyforge.org](http://facets.rubyforge.org)

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [December 16, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/an-idiomatic-way-to-traverse-all-pairs-in-an-enumerable/123288/6 "2007-12-16T21:05:01Z")

</div>

Trans:

> On Dec 15, 6:03 pm, “Shot (Piotr S.)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > In general, I’m looking for an idiomatic way of traversing  
> > each possible pair of a given enum’s contents (once).

> Facets exits for you.

(I canâ€™t help but love the typo.)

> [http://facets.rubyforge.org/rdoc/core/classes/Enumerable.html#M000450](http://facets.rubyforge.org/rdoc/core/classes/Enumerable.html#M000450)

Ah, thanks â€“ I have both Facetsâ€™ and Utility Beltâ€™s documentation  
on my to-read list, but didnâ€™t have the time yet. 🙂

– Shot

---

<div class="post-metadata">

**Author:** ![Shot\_SPiotr\_S.S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@Shot\_SPiotr\_S.S](https://www.ruby-forum.com/u/Shot_SPiotr_S.S)\
**Post date:** [December 16, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/an-idiomatic-way-to-traverse-all-pairs-in-an-enumerable/123288/7 "2007-12-16T16:06:25Z")

</div>

Rob B.:

> On Dec 15, 2007, at 6:03 PM, Shot (Piotr S.) wrote:

> > In general, Iâ€™m looking for an idiomatic way of traversing  
> > each possible pair of a given enumâ€™s contents (once).

> [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/42671](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/42671)

> You could use the Array#combination code  
> posted there either directly or as an example.

Thanks a lot!

In a related field, the above touched some obscure part of my brain  
and I recalled the permutation gem: [http://permutation.rubyforge.org/](http://permutation.rubyforge.org/)

> If you can rely on the ordering of elements  
> (your a\>=b), your code isn’t too bad

If I canâ€™t rely on it, I use each\_with\_index:

each\_with\_index do |a, i|  
each\_with\_index do |b, j|  
next if i \>= j  
# do something with a and b  
end  
end

> If you have an enumerable that’s not an Array,  
> you might have some tweaking to do anyway.

The above each\_with\_index works quite well for most  
(all?) enumerables. 🙂 Thanks again for your reply!

– Shot
