Class: Range
- Inherits:
-
Object
show all
- Defined in:
- lib/regioned_calculation.rb
Instance Method Summary
collapse
Instance Method Details
#intersection(other) ⇒ Object
Also known as:
&
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/regioned_calculation.rb', line 24
def intersection(other)
raise ArgumentError, 'value must be a Range' unless other.kind_of?(Range)
raise ArgumentError, 'must be the same end inclusive/non-inclusive type' unless exclude_end? == other.exclude_end?
min, max = first, last
other_min, other_max = other.first, other.last
new_min = self === other_min ? other_min : other === min ? min : min == other_min ? min : nil
new_max = self === other_max ? other_max : other === max ? max : max == other_max ? max : nil
new_min && new_max ? (exclude_end? ? new_min...new_max : new_min..new_max) : nil
end
|
#subtract(range) ⇒ Object
Also known as:
-
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/regioned_calculation.rb', line 37
def subtract(range)
return [ self ] unless range
raise ArgumentError, 'must be the same end inclusive/non-inclusive type' unless exclude_end? == range.exclude_end?
common = self & range
return [ self ] unless common
return [ ] if common == self
if self.end == common.end
return [ Range.new( self.begin, common.begin, exclude_end? ) ]
end
if self.begin == common.begin
return [ Range.new( common.end, self.end, exclude_end? ) ]
end
[ Range.new( self.begin, common.begin, exclude_end? ),
Range.new( common.end, self.end, exclude_end? ) ]
end
|