Skip to content

Instantly share code, notes, and snippets.

@riywo
Created July 5, 2013 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riywo/5937400 to your computer and use it in GitHub Desktop.
Save riywo/5937400 to your computer and use it in GitHub Desktop.
cross multiple arraies
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