From: "Shot (Piotr Szotkowski)" Date: 2007-12-17T00:05:09+09:00 Subject: Re: An idiomatic way to traverse all pairs in an enumerable? --a8Wt8u1KmwUX3Y2C Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Rob Biedenharn: > On Dec 15, 2007, at 6:03 PM, Shot (Piotr Szotkowski) wrote: >> In general, I=E2=80=99m looking for an idiomatic way of traversing >> each possible pair of a given enum=E2=80=99s contents (once). > 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/ > If you can rely on the ordering of elements > (your a>=3Db), your code isn't too bad If I can=E2=80=99t rely on it, I use each_with_index: each_with_index do |a, i| each_with_index do |b, j| next if i >=3D 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 --=20 He's dead, Jim. You take his phaser, I'll get his wallet. --a8Wt8u1KmwUX3Y2C Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFHZTF1i/mCfdEo8UoRAlrtAJ9oF/f9LTRB/bfBRfWExB7uNn+VkwCfYPgY nWn26C4avkweC88tMJEXL+4= =0e8+ -----END PGP SIGNATURE----- --a8Wt8u1KmwUX3Y2C--