Class: CartesianProduct

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cartesian-product.rb

Instance Method Summary collapse

Constructor Details

#initialize(*arrays) ⇒ CartesianProduct

Returns a new instance of CartesianProduct.



4
5
6
7
# File 'lib/cartesian-product.rb', line 4

def initialize(*arrays)
  @arrays = arrays.reverse
  @num_elements = @arrays.inject(1){ |accum, item| accum *= item.length }
end

Instance Method Details

#countObject



9
10
11
# File 'lib/cartesian-product.rb', line 9

def count
  @num_elements
end

#each(start_index = 0, stop_index = -1)) ⇒ Object



13
14
15
16
17
# File 'lib/cartesian-product.rb', line 13

def each(start_index=0, stop_index=-1)
  start_index = @num_elements + start_index if start_index < 0
  stop_index = @num_elements + stop_index + 1 if stop_index < 0
  (start_index...stop_index).each { |index| yield(index_to_item(index)) }
end