How to Combo

Easiest implementation of “some_method” in Ruby?

 a = %w|a b|
 b = %w|a x|
 c = %w|x y|

some_method(a,b,c)

#=>

[ [“a”, “a”, “x”]
[“a”, “a”, “y”]
[“a”, “x”, “x”]
[“a”, “x”, “y”]
[“b”, “a”, “x”]
[“b”, “a”, “y”]
[“b”, “x”, “x”]
[“b”, “x”, “y”] ]

Order doesn’t really matter.

On Sun, Dec 25, 2011 at 10:27 PM, Intransition [email protected]
wrote:

[ [“a”, “a”, “x”]
[“a”, “a”, “y”]
[“a”, “x”, “x”]
[“a”, “x”, “y”]
[“b”, “a”, “x”]
[“b”, “a”, “y”]
[“b”, “x”, “x”]
[“b”, “x”, “y”] ]

Order doesn’t really matter.

This seems to do it.
I don’t have time now to try something better.

Hope it helps.

def some_method(a,b,c)
[].tap{|w| a.each{|x| b.each{|y| c.each{|z| w << [x,y,z]}}}}
end

Harry

def some_method(a,b,c)
a.product(b, c)
end

I think it only works on 1.9.

– Matma R.

Thomas S. wrote in post #1038150:

Easiest implementation of “some_method” in Ruby?

 a = %w|a b|
 b = %w|a x|
 c = %w|x y|

some_method(a,b,c)

#=>

[ [“a”, “a”, “x”]
[“a”, “a”, “y”]
[“a”, “x”, “x”]
[“a”, “x”, “y”]
[“b”, “a”, “x”]
[“b”, “a”, “y”]
[“b”, “x”, “x”]
[“b”, “x”, “y”] ]

Order doesn’t really matter.

Does some_method only accept exactly three arguments?
Is each argument an array of exactly two elements?

On Monday, December 26, 2011 4:36:26 AM UTC-5, Brian C. wrote:

Does some_method only accept exactly three arguments?
Is each argument an array of exactly two elements?

Yes, Good questions. I should have made it clear this was an arbitrary
example and any number of arguments, each with any number of elements
ought
to work too.

Thankfully, the simple solution, which I was pretty sure was there but
couldn’t recall, was presented: Ruby has Array#product.

Ah, that’s what I had over looked… the product!

Thank you.