Parent

Included Modules

Range

Public Class Methods

combine(*intervals) click to toggle source

Combine intervals.

  Range.combine(1..2, 2..4)   #=> [1..4]
  Range.combine(1..2, 3..4)   #=> [1..2, 3..4]

CREDIT: Trans

    # File lib/core/facets/range/combine.rb, line 23
23:   def self.combine(*intervals)
24:     intype = intervals.first.class
25:     result = []
26: 
27:     intervals = intervals.collect do |i|
28:       [i.first, i.last]
29:     end
30: 
31:     intervals.sort.each do |(from, to)|  #inject([]) do |result,
32:       if result.empty? or from > result.last[1]
33:         result << [from, to]
34:       elsif to > result.last[1]
35:         result.last[1] = to
36:       end
37:       #result
38:     end
39: 
40:     if intype <= Range
41:       result.collect{ |i| ((i.first)..(i.last)) }
42:     else
43:       result
44:     end
45:   end

Public Instance Methods

combine(*intervals) click to toggle source

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

    # File lib/core/facets/range/combine.rb, line 12
12:   def combine(*intervals)
13:     Range.combine(self, *intervals)
14:   end
overlap?(other) click to toggle source

Do two ranges overlap?

CREDIT: Daniel Schierbeck, Brandon Keepers

   # File lib/core/facets/range/overlap.rb, line 7
7:   def overlap?(other)
8:     include?(other.first) or other.include?(first)
9:   end
to_r() click to toggle source

A thing really should know itself. This simply returns self.

CREDIT: Trans

    # File lib/core/facets/range/to_r.rb, line 8
 8:   def to_r
 9:     self
10:   end
to_range() click to toggle source

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

    # File lib/core/facets/range/to_r.rb, line 21
21:   def to_range
22:     self
23:   end
umbrella(r) click to toggle source

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, Chris Kappler

    # File lib/core/facets/range/within.rb, line 49
49:   def umbrella(r)
50:     s = first <=> r.first
51:     e = r.last <=> last
52: 
53:     if e == 0
54:       if r.exclude_end? and exclude_end?
55:         e = r.max <=> max
56:       else
57:         e = (r.exclude_end? ? 0 : 1) <=> (exclude_end? ? 0 : 1)
58:       end
59:     end
60: 
61:     return s,e
62:   end
within?(rng) click to toggle source

Uses the Range#umbrella method to determine if another Range is anywhere within this Range.

  (1..3).within?(0..4)  #=> true

CREDIT: Trans

    # File lib/core/facets/range/within.rb, line 10
10:   def within?(rng)
11:     case rng.umbrella(self)
12:     when [0,0], [1,0], [0,1], [1,1]
13:       return true
14:     else
15:       return false
16:     end
17:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.