Sha256: 5ebcc173f12edb423e184fcf8c1992f76202950668652c7408dd1437429c3a75
Contents?: true
Size: 689 Bytes
Versions: 3
Compression:
Stored size: 689 Bytes
Contents
class Array if RUBY_VERSION < '1.9' # 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 # Operator alias for cross-product. # # a = [1,2] ** [4,5] # a #=> [[1, 4],[1, 5],[2, 4],[2, 5]] # # CREDIT: Trans alias_method :**, :product end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
facets-2.6.0 | lib/core/facets/array/product.rb |
facets-2.5.1 | lib/core/facets/array/product.rb |
facets-2.5.2 | lib/core/facets/array/product.rb |