Skip to content

Instantly share code, notes, and snippets.

@Papillard
Created July 17, 2013 14:10
Show Gist options
  • Save Papillard/6020874 to your computer and use it in GitHub Desktop.
Save Papillard/6020874 to your computer and use it in GitHub Desktop.
yielding cartesian product elements..
class CartesianProduct
include Enumerable
def initialize a,b
@a,@b = a,b
end
def each
@a.each do |a_element|
@b.each do |b_element|
yield [a_element,b_element]
end
end
end
end
c = CartesianProduct.new([1, 2],[:hehe, :tareum, 6])
c.each {|el| p el}
c = CartesianProduct.new([:a,:b], [])
c.each {|el| p el}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment