method

overlaps?

v3.2.13 - Show latest stable - Class: Range
overlaps?(other)
public

Compare two ranges and see if they overlap each other

(1..5).overlaps?(4..6) # => true
(1..5).overlaps?(7..9) # => false

3Notes

Take care with time ranges

julescopeland · Nov 13, 2013

Trying this in the console:

(1.day.from_now..5.days.from_now).overlaps?(5.days.from_now..10.days.from_now)

will blow up...

It's fine with Dates though:

(1.day.from_now.to_date..5.days.from_now.to_date).overlaps?(5.days.from_now.to_date..10.days.from_now.to_date) # => true

Adjacency

julescopeland · Nov 13, 2013

Just for completeness (it should behave like this), comparing ranges that start and end with the same value will overlap, e.g.

(1..5).overlaps?(5..10) # => true

If you need to check for overlaps in Times, use the validates_overlap gem.

joshuapinter · May 3, 2018