Issue #14473 has been updated by owst (Owen Stephens).


Good point v.ox, I did consider implementing `subset?` as an overloading of `include?` (or possibly `cover?` ?), as one can't have a range-of-ranges (so there is no ambiguity).

How would you distinguish between strict/non-strict - a `strict:` kwarg, perhaps? Something like:

~~~ruby
(1..3).cover?((1..3)) # => true
(1..3).cover?((1..3), strict: false) # => true
(1..3).cover?((1..3), strict: true) # => false
(1..3).cover?((1..2), strict: true) # => true
~~~

I'm not sure the level of justification required for a method to be included in the stdlib, but I can provide an example where we would (and do) use `Range#subset?`: we have products that have a range of allowed values, and in some circumstances we restrict products to a dynamically-generated range of allowed values, and want to make sure that range is compatible with the product range, something like:

~~~ruby
raise ArgumentError unless dynamic_value_range.subset?(product_value_range)
~~~

The other methods (`strict_subset?`, and the `*superset?` methods) were only added for symmetry.

----------------------------------------
Feature #14473: Add Range#subrange?
https://bugs.ruby-lang.org/issues/14473#change-70630

* Author: greggzst (Grzegorz Jakubiak)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Hi there,

I'd like to propose a method that returns true when a range that the method gets called on is a subrange of a range passed in as an argument.

Example:

~~~ ruby
(2..4).subrange?(1...4) 
=> true
(-2..2).subrange?(-1..3) 
=> false
~~~




---Files--------------------------------
0001-range.c-add-subset-superset-methods.patch (8.84 KB)
v2-0001-range.c-add-subset-superset-methods.patch (8.85 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>