I'm a React, Javascript & Rails full-stack Software Engineer. I have been working remotely for the past two years in a remote village. Before joining as a Software Engineer I founded kwiq - a hyperlocal delivery startup to deliver things in remote villages.
2 minute read
In Ruby, a Range is an object that represents a range of values with a defined beginning and end. It’s a fundamental data structure used to express a sequence or span between two values, whether they are numeric, alphabetical, or even dates.
We have two ways to define ranges.
Rails Range#overlaps?(range) method
Compare two ranges and see if they overlap each other. overlaps? returns true if two ranges overlap each other otherwise false.
Before Ruby 3.3
Unlike Rails, Ruby does not have any method names as overlap? to compare two ranges and see if they overlap each other.
Prior to Ruby 3.3, if we wanted to check for overlaps. We would need to use the cover method.
It would return true if the two ranges overlap each other.
There is a problem with it. It doesn’t work with open ranges.
After
Ruby 3.3 introduces Range#overlap?(range) method. Like Rails, we can use overlap? method to compare two ranges
and see if they overlap each other.
It returns true if two ranges overlap each other otherwise false.