Skip to content

Instantly share code, notes, and snippets.

Created October 15, 2012 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3894574 to your computer and use it in GitHub Desktop.
Save anonymous/3894574 to your computer and use it in GitHub Desktop.
class CartesianProduct
include Enumerable
def initalize(x, y)
@x = x
@y = y
end
def each
return to_enum unless block_given?
@x.each do |x|
@y.each do |y|
yield [x, y]
end
end
end
end
c = CartesianProduct.new([:a,:b], [4,5])
c.each { |elt| puts elt.inspect }
c = CartesianProduct.new([:a,:b], [])
c.each { |elt| puts elt.inspect }
error message
cartesian.rb:10:in `each': undefined method `each' for nil:NilClass (NoMethodError)
from cartesian.rb:20:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment