Class: Range

Inherits:
Object show all
Defined in:
lib/nano/range/to_r.rb,
lib/nano/range/to_range.rb,
lib/nano/range/umbrella.rb,
lib/nano/range/within%3F.rb

Instance Method Summary collapse

Instance Method Details

#to_rObject

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



5
6
7
# File 'lib/nano/range/to_r.rb', line 5

def to_r
  self
end

#to_rangeObject

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.



9
10
11
# File 'lib/nano/range/to_range.rb', line 9

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]


31
32
33
34
35
# File 'lib/nano/range/umbrella.rb', line 31

def umbrella(r)
  s = self.first <=> r.first
  e = r.last <=> self.last
  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

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
# File 'lib/nano/range/within%3F.rb', line 10

def within?(rng)
  case rng.umbrella(self)
  when [0,0], [-1,0], [0,-1], [-1,-1]
    return true
  else
    return false
  end
end