Class: Range

Inherits:
Object
  • Object
show all
Defined in:
lib/is-range.rb

Instance Method Summary collapse

Instance Method Details

#&(other) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/is-range.rb', line 5

def & other
  return nil unless overlap?(other)
  b, e, x = self.begin, self.end, self.exclude_end?
  bb, ee, xx = other.begin, other.end, other.exclude_end?
  rb = if !bb || (b && b > bb)
      b
    else
      bb
    end
  re, rx = if !ee || (e && e < ee)
    [e, x]
  else
    [ee, xx]
  end
  Range::new rb, re, rx
end

#empty?Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/is-range.rb', line 41

def empty?
  return false if self.begin.nil? || self.end.nil?
  self.begin > self.end || (self.exclude_end? && self.begin == self.end)
end

#|(other) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/is-range.rb', line 22

def | other
  return nil unless overlap?(other) || self.end_succ == other.begin || self.begin == other.end_succ
  b, e, x = self.begin, self.end, self.exclude_end?
  bb, ee, xx = other.begin, other.end, other.exclude_end?
  rb = if !b || (bb && b < bb)
    b
  else
    bb
  end
  re, rx = if !e || (ee && e > ee)
    [e, x]
  elsif e == ee
    [e, x && xx]
  else
    [ee, xx]
  end
  Range::new rb, re, rx
end