Created
October 15, 2012 19:20
-
-
Save anonymous/3894574 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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