Mega Code Archive

 
Categories / Ruby / Array
 

Array cartesian

module Enumerable   def cartesian(other)     res = []     each { |x| other.each { |y| res << [x, y] } }     return res   end end p [1,2,3].cartesian(['a',5,6]) # => [[1, "a"], [1, 5], [1, 6], # =>  [2, "a"], [2, 5], [2, 6], # =>  [3, "a"], [3, 5], [3, 6]]