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 Rails@fa9d01d

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

# => true

For Rails <= 4.2.7

# => 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)

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

After Rails@fa9d01d

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


(1..5) === (1..5)
# => true
(1..5) === (2..3)
# => true
(1..5) === (2..6)
# => false


# => true
# => true
# => false


# => true
# => true
# => 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.

Akshay Mohite

Hi there! I am a Ruby on Rails & ReactJS Enthusiast, building some cool products at DTree Labs.

Read More
Buy me a coffee