From: Ralf Mueller Date: 2012-04-12T22:08:31+09:00 Subject: Re: ruby v.1.8.6. Expand array class, make combinations On 04/12/2012 10:10 AM, Ronnie Aa wrote: > Hi, > > I'm using Sketchups' ruby; its version: 1.8.6. Sketchup is a 3D design > program which has as ruby built in to perform all kinds of operations on > geometry.I'm not able to use higher versions of ruby. > > My question: > > > > This is what I want to accomplish: > > ["a","b","c","d"].permutation(2) > > ==> [ [a,b],[a,c],[a,d],[b,c],[b,d],[c,d] ] > but it's not available for me > > > > I also found: > > http://permutation.rubyforge.org/. I succeeded to install the gem, > but I couldn't accomplish the above. > > > > > So I've created my own method: > > class Array > def comby ## Make al unique combinations of 2 elements > > posmax = (length - 1) > > > newarr = [] > > > > for n in 0.. posmax > for m in 0.. posmax > i1 = self[n] > i2 = self[m] > if newarr.include? ([i2,i1]) > # newarr.push('X') > elsif i1 == i2 > # newarr.push('X') > else > newarr.push([i1,i2]) > end > end > end > puts newarr[0..14] > ==> [ [a,b],[a,c],[a,d],[b,c],[b,d],[c,d] ] > > end > > end > > //program > > mm = ["a","b","c","d"] > > mm.comby > > // > > > > > It puts the right values, but what I can't use newarr ( tried also > @newarr) > in my program. > > What would like to see: > > mm = ["a","b","c","d"] > aa = mm.comby > puts aa > ==> [ [a,b],[a,c],[a,d],[b,c],[b,d],[c,d] ] > > What can I do?? > > > Thanks! > What about ruby facets: http://rubyworks.github.com/rubyfaux/?doc=http://rubyworks.github.com/facets/docs/facets-2.9.3/core.json#api-class-Array/api-method-Array-h-combination There is a combination method for the Array class. does it fit your needs? ralf