Class: Range
- Inherits:
-
Object
- Object
- Range
- Defined in:
- lib/combinatorics/extensions/range.rb
Instance Method Summary collapse
-
#&(other) ⇒ Range
Finds the intersecting sub-range.
-
#downto(other) {|subrange| ... } ⇒ Enumerator
Iterates over every sub-range down to the other range.
-
#upto(other) {|subrange| ... } ⇒ Enumerator
Iterates over every sub-range up to the other range.
Instance Method Details
#&(other) ⇒ Range
Finds the intersecting sub-range.
18 19 20 21 22 23 |
# File 'lib/combinatorics/extensions/range.rb', line 18 def &(other) Range.new( [self.first, other.first].max, [self.last, other.last].min ) end |
#downto(other) {|subrange| ... } ⇒ Enumerator
Iterates over every sub-range down to the other range.
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/combinatorics/extensions/range.rb', line 83 def downto(other) return enum_for(:downto,other) unless block_given? unless other.kind_of?(Range) raise(TypeError,"bad value for range",caller) end self.first.downto(other.first) do |start| self.last.downto(other.last) do |stop| yield start..stop end end end |
#upto(other) {|subrange| ... } ⇒ Enumerator
Iterates over every sub-range up to the other range.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/combinatorics/extensions/range.rb', line 47 def upto(other) return enum_for(:upto,other) unless block_given? unless other.kind_of?(Range) raise(TypeError,"bad value for range",caller) end self.first.upto(other.first) do |start| self.last.upto(other.last) do |stop| yield start..stop end end end |