Ruby v.1.8.6. Expand array class, make combinations

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!

Hi,

What do you mean by “use newarr”? If you want the method to return the
result array, you have to write “return newarr” (or simply “newarr”) in
the method body:

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
newarr # return the result array
end
end

By the way, I would replace the “for … in” loops with “upto”
iterators. Those or more Ruby-like and have the advantage of not
spilling every internal variable. And the inner loop/iterator shouldn’t
start at 0 but at the next to current index of the outer loop/iterator.
Then you can leave out the if statement (assuming the base array is
unique):

0.upto posmax do |i|
(i + 1).upto posmax do |j|
newarr << [self[i], self[j]]
end
end

0.upto posmax do |i|
(i + 1).upto posmax do |j|
newarr << [self[i], self[j]]
end
end

To advanced for newbie!! :slight_smile: Thnx for your help!

On 04/12/2012 10:10 AM, Ronnie Aa wrote:

This is what I want to accomplish:
http://permutation.rubyforge.org/. I succeeded to install the gem,
posmax = (length - 1)
if newarr.include? ([i2,i1])

//
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

On 04/12/2012 03:41 PM, Ronnie Aa wrote:

Hi Ralf,

Thanx for pointing me towards this good collection called ‘facets’.
But is there any difference in this case, just use some code…

I have no idea, how much your code is tested. But if you’re new to ruby
and you just need this functionality,
why not use a well known library like facets? Of course you could do it
yourself to avoid an external
dependency. Unless this combination is a central thing in your
application, I would let facets do the job and
concentrate on the hard part the problem.

ralf

Hi Ralf,

Thanx for pointing me towards this good collection called ‘facets’.
But is there any difference in this case, just use some code…

Ok, I think you’re right! I’m still new to ruby and programming ig,
so every piece of information is welcome…

Well, if you really just need this one method, I think installing a
complete library is a bit too much. I mean, this is no special algorithm
but a very basic operation that you can program ad hoc.