Created
July 5, 2013 21:33
-
-
Save riywo/5937400 to your computer and use it in GitHub Desktop.
cross multiple arraies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def cross(array) | |
product(array).map { |e| e.flatten } | |
end | |
end | |
p [1,2].cross([3,4]) | |
#=> [[1, 3], [1, 4], [2, 3], [2, 4]] | |
p [1,2].cross([3,4]).cross([5,6]) | |
#=> [[1, 3, 5], [1, 3, 6], [1, 4, 5], [1, 4, 6], [2, 3, 5], [2, 3, 6], [2, 4, 5], [2, 4, 6]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment