Class: My::Combination

Inherits:
Object
  • Object
show all
Defined in:
lib/my/combination.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arrays, start: 0, stop: nil) ⇒ Combination

范围包含 start,不包含 stop

Raises:

  • (IndexError)


9
10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



6
7
8
# File 'lib/my/combination.rb', line 6

def index
  @index
end

#lengthObject (readonly)

Returns the value of attribute length.



6
7
8
# File 'lib/my/combination.rb', line 6

def length
  @length
end

Instance Method Details

#diff_countObject



37
38
39
# File 'lib/my/combination.rb', line 37

def diff_count
  @index - @start
end

#succ!Object

Raises:

  • (StopIteration)


27
28
29
30
31
# File 'lib/my/combination.rb', line 27

def succ!
  raise StopIteration unless succ?
  @index += 1
  nil
end

#succ?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/my/combination.rb', line 33

def succ?
  @index < @stop - 1
end

#valuesObject



19
20
21
22
23
24
25
# 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