In order to compute the cartesian product in Ruby, one can use Array#product
, how is the syntax if I have an array of arrays and want to compute the product?
[[1,2],[3,4],[5,6]] => [[1,3,5], [2,3,5], ...]
I am not sure, because in the Ruby documentation the product
method is defined with an arbitrary number of arguments, so simply passing my arrays of arrays as an argument, like that:
[].product(as) => [
does not suffice. How can I solve this?
The method takes multiple arguments, but not an array containing arguments. So you have to use it in this way:
[1,2].product [3,4], [5,6]
If as
is your array of arrays, you will have to "splat" it like this:
as[0].product(*as[1..-1])
Closest notation I've got is:
:product.to_proc.call(*as)
# shorthand
:product.to_proc.(*as)
:product.to_proc[*as]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With