require 'generator' module Enumerable class Product include Enumerable def initialize(first, second) @first = first @second = second end def each xg = Generator.new(@first) yg = Generator.new(@second) xa = Array.new ya = Array.new n = 0 loop{ xa << xg.next unless xg.end? ya << yg.next unless yg.end? break if xa.size + ya.size - 2 < n n.downto(0){|x| y = n - x yield(xa[x], ya[y]) if x < xa.size and y < ya.size } n = n.succ } end def member?(obj) obj.is_a?(Array) and @first.member?(obj[0]) and @second.member?(obj[1]) end alias include? member? alias === member? end end