Sha256: f9d24331764f206d824f933f4260fc487590c57641003163a9c15113baf8bad9

Contents?: true

Size: 533 Bytes

Versions: 7

Compression:

Stored size: 533 Bytes

Contents

class Array

  unless method_defined?(:product) # 1.8.7+

    # Provides the cartesian product of two or more arrays.
    #
    #   a = [1,2].product([4,5])
    #   a  #=> [[1, 4],[1, 5],[2, 4],[2, 5]]
    #
    # CREDIT: Thomas Hafner

    def product(*enums)
      enums.unshift self
      result = [[]]
      while [] != enums
        t, result = result, []
        b, *enums = enums
        t.each do |a|
          b.each do |n|
            result << a + [n]
          end
        end
      end
      result
    end

  end

end

Version data entries

7 entries across 6 versions & 1 rubygems

Version Path
facets-2.9.3 lib/core/facets/array/product.rb
facets-2.9.2 lib/core/facets/array/product.rb
facets-2.9.2 src/core/facets/array/product.rb
facets-2.9.1 lib/core/facets/array/product.rb
facets-2.9.0 lib/core/facets/array/product.rb
facets-2.9.0.pre.2 lib/core/facets/array/product.rb
facets-2.9.0.pre.1 lib/core/facets/array/product.rb