Class: My::Combination
- Inherits:
-
Object
- Object
- My::Combination
- Defined in:
- lib/my/combination.rb
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
Instance Method Summary collapse
- #diff_count ⇒ Object
-
#initialize(arrays, start: 0, stop: nil) ⇒ Combination
constructor
范围包含 start,不包含 stop.
- #succ! ⇒ Object
- #succ? ⇒ Boolean
- #values ⇒ Object
Constructor Details
#initialize(arrays, start: 0, stop: nil) ⇒ Combination
范围包含 start,不包含 stop
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
#index ⇒ Object (readonly)
Returns the value of attribute index.
6 7 8 |
# File 'lib/my/combination.rb', line 6 def index @index end |
#length ⇒ Object (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_count ⇒ Object
37 38 39 |
# File 'lib/my/combination.rb', line 37 def diff_count @index - @start end |
#succ! ⇒ Object
27 28 29 30 31 |
# File 'lib/my/combination.rb', line 27 def succ! raise StopIteration unless succ? @index += 1 nil end |
#succ? ⇒ Boolean
33 34 35 |
# File 'lib/my/combination.rb', line 33 def succ? @index < @stop - 1 end |
#values ⇒ Object
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 |