Advertisement
EDSE

Ruby yield - SaaS Course - Home Work 01, part 6 a.

May 30th, 2012
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.48 KB | None | 0 0
  1. class CartesianProduct
  2.   include Enumerable
  3.   @@p
  4.   def initialize(arg1, arg2)
  5.     @@p = arg1.product arg2
  6.   end
  7.   def each
  8.     @@p.each do |elt| yield elt end
  9.   end
  10. end
  11. #Examples of use
  12. c = CartesianProduct.new([:a,:b], [4,5])
  13. c.each { |elt| puts elt.inspect }
  14. # [:a, 4]
  15. # [:a, 5]
  16. # [:b, 4]
  17. # [:b, 5]
  18. c = CartesianProduct.new([:a,:b], [])
  19. c.each { |elt| puts elt.inspect }
  20. # (nothing printed since Cartesian product
  21. # of anything with an empty collection is empty)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement