class My::Combination

Attributes

index[R]
length[R]

Public Class Methods

new(arrays, start: 0, stop: nil) click to toggle source

范围包含 start,不包含 stop

# File lib/my/combination.rb, line 9
def initialize arrays, start: 0, stop: nil
  @arrays = arrays
  @length = arrays.reduce(1){|n, a| a.size * n }
  @start = start
  @index = start
  @stop = stop || length
  @stop = length if @stop > length
  raise IndexError if @stop <= @start
end

Public Instance Methods

diff_count() click to toggle source
# File lib/my/combination.rb, line 37
def diff_count
  @index - @start
end
succ!() click to toggle source
# File lib/my/combination.rb, line 27
def succ!
  raise StopIteration unless succ?
  @index += 1
  nil
end
succ?() click to toggle source
# File lib/my/combination.rb, line 33
def succ?
  @index < @stop - 1
end
values() click to toggle source
# File lib/my/combination.rb, line 19
def values
  i = @index
  @arrays.map do |array|
    i, n = i.divmod(array.size)
    array[n]
  end
end