Class: Range
- Defined in:
- lib/core/facets/conversion.rb,
lib/core/facets/range/combine.rb,
lib/core/facets/range/overlap.rb
Class Method Summary collapse
-
.combine(*intervals) ⇒ Object
Combine intervals.
Instance Method Summary collapse
-
#combine(*intervals) ⇒ Object
Combine ranges.
-
#overlap?(other) ⇒ Boolean
Do two ranges overlap?.
-
#to_r ⇒ Object
A thing really should know itself.
-
#to_range ⇒ Object
A thing really should know itself.
-
#umbrella(r) ⇒ Object
Returns a two element array of the relationship between two Ranges.
-
#within?(rng) ⇒ Boolean
Uses the Range#umbrella method to determine if another Range is anywhere within this Range.
Class Method Details
.combine(*intervals) ⇒ Object
Combine intervals.
Range.combine(1..2, 2..4) #=> [1..4]
Range.combine(1..2, 3..4) #=> [1..2, 3..4]
CREDIT: Trans
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/core/facets/range/combine.rb', line 10 def self.combine(*intervals) intype = intervals.first.class result = [] intervals = intervals.collect do |i| [i.first, i.last] end intervals.sort.each do |(from, to)| #inject([]) do |result, if result.empty? or from > result.last[1] result << [from, to] elsif to > result.last[1] result.last[1] = to end #result end if intype <= Range result.collect{ |i| ((i.first)..(i.last)) } else result end end |
Instance Method Details
#combine(*intervals) ⇒ Object
Combine ranges.
(1..2).combine(2..4) #=> [1..4]
(1..2).combine(3..4) #=> [1..2, 3..4]
TODO: Incorporate end-sentinal inclusion vs. exclusion.
CREDIT: Trans
43 44 45 |
# File 'lib/core/facets/range/combine.rb', line 43 def combine(*intervals) Range.combine(self, *intervals) end |
#overlap?(other) ⇒ Boolean
Do two ranges overlap?
CREDIT: Daniel Schierbeck
CREDIT: Brandon Keepers
8 9 10 |
# File 'lib/core/facets/range/overlap.rb', line 8 def overlap?(other) include?(other.first) or other.include?(first) end |
#to_r ⇒ Object
A thing really should know itself. This simply returns self.
CREDIT: Trans
266 267 268 |
# File 'lib/core/facets/conversion.rb', line 266 def to_r self end |
#to_range ⇒ Object
A thing really should know itself. This simply returns self.
Note: This does not internally effect the Ruby interpretor such that it can coerce Range-like objects into a Range.
CREDIT: Trans
279 280 281 |
# File 'lib/core/facets/conversion.rb', line 279 def to_range self end |
#umbrella(r) ⇒ Object
Returns a two element array of the relationship between two Ranges.
Diagram:
Relationship Returns
self |-----------|
r |-----------| [0,0]
self |-----------|
r |---------| [-1,-1]
self |---------|
r |-----------| [1,1]
self |-----------|
r |----------| [-1,0]
self |-----------|
r |-----------| [-1,1]
etc.
Example:
(0..3).umbrella(1..2) #=> [-1,-1]
CREDIT: Trans
CREDIT: Chris Kappler
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/core/facets/range/overlap.rb', line 43 def umbrella(r) s = first <=> r.first e = r.last <=> last if e == 0 if r.exclude_end? and exclude_end? e = r.max <=> max else e = (r.exclude_end? ? 0 : 1) <=> (exclude_end? ? 0 : 1) end end return s,e end |
#within?(rng) ⇒ Boolean
Uses the Range#umbrella method to determine if another Range is anywhere within this Range.
(1..3).within?(0..4) #=> true
CREDIT: Trans
65 66 67 68 69 70 71 72 |
# File 'lib/core/facets/range/overlap.rb', line 65 def within?(rng) case rng.umbrella(self) when [0,0], [-1,0], [0,-1], [-1,-1] return true else return false end end |