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 in v. 1.8.6
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 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!