Class: Range

Inherits:
Object
  • Object
show all
Defined in:
lib/spice_utils.rb

Overview

We need to add some functionality to Range for wndifd

Instance Method Summary (collapse)

Instance Method Details

- (nil, Range) intersection(other)

Find the intersection of two ranges. Used by Spice#wndifd

Parameters:

  • other (Range)

    Range to compare with self

Returns:

  • (nil)

    if no intersection

  • (Range)

    of intersection



115
116
117
118
119
120
121
122
123
# File 'lib/spice_utils.rb', line 115

def intersection(other)
  my_min, my_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 === my_min ? my_min : nil
  new_max = self === other_max ? other_max : other === my_max ? my_max : nil

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