Skip to content

Instantly share code, notes, and snippets.

@KarenWest
Created May 1, 2013 21:25
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 KarenWest/5498501 to your computer and use it in GitHub Desktop.
Save KarenWest/5498501 to your computer and use it in GitHub Desktop.
module Enumerable
def initialize(seq1, seq2)
puts "Enumerable initialize"
puts "seq1"
puts seq1
puts "seq2"
puts seq2
@seq1 = seq1
@seq2 = seq2
#return @seq1,@seq2
#cartesion_product(seq1,seq2)
end
#def method_missing(seq1,seq2)
#puts "Enumerable method_missing"
#end
def cartesion_product(seq_1,seq_2)
puts "cartesion_product(seq_1,seq_2)"
puts seq_1
puts seq_2
seq_1.each do |elt1|
puts "elt1"
puts elt1
seq_2.each do |elt2|
puts "elt2"
puts elt2
puts "yielding"
puts elt1
puts elt2
yield [elt1,elt2]
end
end
end
end
class CartesianProduct
include Enumerable
# YOUR CODE HERE
# LOOK UP - way to represent variable # of args to method_missing()?
def method_missing(enumObj)
puts "method_missing() for CartesionProduct class--enumerable object"
puts "enumObj seq1"
puts enumObj.@seq1
puts "enumObj seq2"
puts enumObj.@seq2
#puts "seq3"
#puts seq3
listcartProd = []
cartCoord = cartesion_product(enumObj.@seq1,enumObj.@seq2)
puts cartCoord
#listcartProd.concat([x,y])
return cartCoord
end
end
#Example test cases:
c = CartesianProduct.new([:a,:b], [4,5])
c.each { |elt| puts elt.inspect }
# [:a, 4]
# [:a, 5]
# [:b, 4]
# [:b, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment