Method: Range#intersection

Defined in:
lib/sugar-high/range.rb

#intersection(other) ⇒ Object

Raises:

  • (ArgumentError)


2
3
4
5
6
7
8
9
10
11
12
# File 'lib/sugar-high/range.rb', line 2

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

  min, max = first, exclude_end? ? max : last  
  other_min, other_max = other.first, other.exclude_end? ? other.max : other.last  

  new_min = self === other_min ? other_min : other === min ? min : nil  
  new_max = self === other_max ? other_max : other === max ? max : nil  

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