From: Martin DeMello Date: 2004-07-14T02:57:21+09:00 Subject: Re: Ruby quickies and useful idioms Gavin Kistner wrote: > On Jul 12, 2004, at 1:47 AM, Martin DeMello wrote: > > I've found this useful on several occasions > > > > class Array > > include EnumerableOperator > > def all_pairs > > a = size-1 > > for i,j in product(0..a, 0..a, proc {|i,j| i < j}) > > yield at(i), at(j) > > end > > end > > end > > It's too early in the morning for me to decode this. Is this the same > as my Array#each_unique_pair method documented here: > > http://phrogz.net/RubyLibs/rdoc/classes/Array.html#M000061 > > If so, the implementation which doesn't require anything else is simply: > > class Array > def each_unique_pair > self.each_with_index{ |a,i| > self[(i+1)..-1].each{ |b| yield a,b } > } > end > end Heh - so it is :) Mine was basically the translation from the list comprehension. martin