Rails Range cover? includes? methods support Range argument

Rails CompareWithRange core extension adds support of Range as an argument to Range#===, Range#cover? and Range#includes? methods. Ruby already has support for these three methods, but ruby does not support Range value as an argument. This change is targetted to release and deprecate old active_support/core_ext/range/include_range with Rails 6.1.

Prior to [email protected]

Range did support value as a Range argument for the method Range#include?. Till then, native implementation of Range#include did not support value as a range argument.

For Rails > 4.2.7

(1..5).include?(1..5)
# => true

For Rails <= 4.2.7

(1..5).include_with_range?(1..5)
# => true

Check the include_with_range documentation for more details.

Source code:

  # This was `include_with_range?` for Rails <= 4.2.7
  def include?(value)
    if value.is_a?(::Range)
      # 1...10 includes 1..9 but it does not include 1..10.
      operator = exclude_end? && !value.exclude_end? ? :< : :<=
      include_without_range?(value.first) && value.last.send(operator, last)
    else
      include_without_range?(value)
    end
  end

Rails keeps default behavior untouched. Relevant source code of Range#include? from Rails ActiveSupport can be seen below.

After [email protected]

Two new methods have been defined to have similar methods as we have in Ruby 2.6.

Range#===
(1..5) === (1..5)
# => true
(1..5) === (2..3)
# => true
(1..5) === (2..6)
# => false
Range#include?
(1..5).include?(1..5)
# => true
(1..5).include?(2..3)
# => true
(1..5).include?(2..6)
# => false
Range#cover?
(1..5).cover?(1..5)
# => true
(1..5).cover?(2..3)
# => true
(1..5).cover?(2..6)
# => false

The commit also added a deprecation waring with include_range file as given below.

You have required `active_support/core_ext/range/include_range`.
This file will be removed in Rails 6.1. You should require `active_support/core_ext/range/compare_range` instead.