Class: Range

Inherits:
Object show all
Defined in:
lib/amp/support/support.rb

Instance Method Summary collapse

Instance Method Details

#intersect(rb) ⇒ Object Also known as: -

Given two ranges return the range where they intersect or None.

intersect((0, 10), (0, 6)) (0, 6) intersect((0, 10), (5, 15)) (5, 10) intersect((0, 10), (10, 15)) intersect((0, 9), (10, 15)) intersect((0, 9), (7, 15)) (7, 9)



524
525
526
527
528
529
530
531
532
533
# File 'lib/amp/support/support.rb', line 524

def intersect(rb)
  ra = self
  start_a = [ra.begin, rb.begin].max
  start_b = [ra.end,   rb.end  ].min
  if start_a < start_b
    start_a..start_b
  else
    nil
  end
end