Module: Torque::Range

Defined in:
lib/torque/range.rb

Instance Method Summary collapse

Instance Method Details

#intersection(other) ⇒ Object Also known as: &

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
# File 'lib/torque/range.rb', line 3

def intersection(other)
  raise ArgumentError, 'value must be a Range' unless other.kind_of?(Range)

  new_min = self.cover?(other.min) ? other.min : other.cover?(min) ? min : nil
  new_max = self.cover?(other.max) ? other.max : other.cover?(max) ? max : nil

  new_min && new_max ? new_min..new_max : nil
end

#union(other) ⇒ Object Also known as: |

Raises:

  • (ArgumentError)


13
14
15
16
17
# File 'lib/torque/range.rb', line 13

def union(other)
  raise ArgumentError, 'value must be a Range' unless other.kind_of?(Range)

  ([min, other.min].min)..([max, other.max].max)
end