dubstep
December 25, 2011, 2:28pm
#1
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.
tcsnide
December 25, 2011, 4:06pm
#2
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
tcsnide
December 25, 2011, 7:49pm
#3
def some_method(a,b,c)
a.product(b, c)
end
I think it only works on 1.9.
– Matma R.
tcsnide
December 26, 2011, 10:36am
#4
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?
tcsnide
December 26, 2011, 3:48pm
#5
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.
tcsnide
December 26, 2011, 2:17am
#6
Ah, that’s what I had over looked… the product!
Thank you.