From: owen@...
Date: 2018-07-31T00:29:37+00:00
Subject: [ruby-core:88222] [Ruby trunk Feature#14473] Add Range#subrange?

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

File v5-0001-range.c-allow-cover-to-accept-Range-argument.patch added

Hi tarui, thank you for reviewing and your suggestion/question. 

In fact, with my v4 patch in the case you describe there is a bug: `(1..3).cover?(1.0...4.0)` is `true` not `false`. This is prevented using `b >= (c...d).max` as you suggest, but I see two issues:

First, the performance of `Range#max` is very bad in certain cases (as al2o3c pointed out above) e.g. on my machine (Macbook Pro, 2.8 GHz i5) I now have: 

~~~
$ time ruby -e "p ('aaaaa'..'zzzzz').cover?('aaaaa'...'zzzzz')"
true
ruby -e "p ('aaaaa'..'zzzzz').cover?('aaaaa'...'zzzzz')"  0.19s user 0.05s system 92% cpu 0.256 total

$ time ruby -e "p ('aaaaa'..'zzzzy').cover?('aaaaa'...'zzzzz')"
true
ruby -e "p ('aaaaa'..'zzzzy').cover?('aaaaa'...'zzzzz')"  8.12s user 0.07s system 98% cpu 8.329 total
~~~
Second, we have the following strange behaviour that is similar to that in your comment:
~~~
$ ruby -e "p (1..4).cover?(1.0...4.0)"
true
$ ruby -e "p (1..3).cover?(1.0...4.0)"
Traceback (most recent call last):
	2: from -e:1:in `<main>'
	1: from -e:1:in `cover?'
-e:1:in `max': cannot exclude non Integer end value (TypeError)
~~~

I have attached an updated patch, which uses `max`, but does not address either of the above issues. Can you see an alternative that addresses these issues in a straightforward way?

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

* Author: greggzst (Grzegorz Jakubiak)
* Status: Assigned
* Priority: Normal
* Assignee: tarui (Masaya Tarui)
* 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)
v3-0001-range.c-add-subrange-superrange-methods.patch (9.24 KB)
v4-0001-range.c-allow-cover-to-accept-Range-argument.patch (4.45 KB)
v5-0001-range.c-allow-cover-to-accept-Range-argument.patch (4.82 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>