Ruby 2.6 Range#cover? accepts Range object as argument

Abhay Nikam

By Abhay Nikam

on October 24, 2018

This blog is part of our  Ruby 2.6 series.

Range#cover? returns true if the object passed as argument is in the range.

2=> true

Range#cover? returns false if the object passed as an argument is non-comparable or is not in the range.

Before Ruby 2.6, Range#cover? used to return false if a Range object is passed as an argument.

1>> (1..10).cover?(2..5)
2=> false

Ruby 2.6

In Ruby 2.6 Range#cover? can accept a Range object as an argument. It returns true if the argument Range is equal to or a subset of the Range.

2=> true
5=> true
8=> false
11=> false

Here is relevant commit and discussion for this change.

If you liked this blog, you might also like the other blogs we have written. Check out the full archive.

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, React.js, React Native, remote work,open source, engineering & design.