From: tom.dalling+ruby-lang@... Date: 2018-03-13T07:36:39+00:00 Subject: [ruby-core:86094] [Ruby trunk Bug#14575] Switch Range#=== to use cover? instead of include? Issue #14575 has been updated by tom_dalling (Tom Dalling). The `Range#===` methods currently works in one of three different ways. The code is here: https://github.com/ruby/ruby/blob/df1cd0f438bd17a4d3fbe9077e0b308e0b25c4b5/range.c#L1151-L1158 1. If it's a range of "numeric values", it uses `#cover?` 2. If it's a range of strings, it uses `rb_str_include_range_p` 3. For everything else, it calls super, which is `Enumerable#include?` I can see that changing the behaviour of (3) might lead to incompatibility issues. Maybe it's better to implement this in (1), by allowing objects to declare themselves as "numeric values". ---------------------------------------- Bug #14575: Switch Range#=== to use cover? instead of include? https://bugs.ruby-lang.org/issues/14575#change-70963 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: * Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN ---------------------------------------- _This is **a conscious duplicate** of the bug I've created [more than a year ago](https://bugs.ruby-lang.org/issues/12612). I believe that the previous one was rejected too easy, mostly due to the fact I haven't provided enough evidence to support my proposal. I also believe that writing the new, better-grounded proposal would be more visible than adding more comments to the rejected ticket._ **The problem**: `Range#===` (used in `case` and `grep`) uses `include?` to check the value against the range, which could be: a) really ineffective or b) simply unavailable. Here are real-life and real-life-alike examples of types that suffer from the problem: * [ipaddress](https://github.com/ipaddress-gem/ipaddress) `IPAddress("172.16.10.1")..IPAddress("172.16.11.255")`: it is really readable to describe in some server config "for this range allow this, for that range allow that", yet it could be fascinatingly slow, calculating thousands of IPs inside range just to check with `include?`; * [Measurement units](https://github.com/joshwlewis/unitwise): `(Unitwise(1, 'm')...Unitwise(10, 'm')) === Unitwise(5, 'm')` throws "can't iterate from Unitwise::Measurement", which is reasonable: there is no `.succ` for numeric types; Ruby itself has an ugly workaround of "if this is a numeric type, behave like `cover?`" * Dates and times: `(Date.today..Date.today + 1) === DateTime.now` is `false`; it is hard to imagine code where it is a desired behavior. Matz's objections to the previous ticket were: > I see no real-world use-case for `Range#===` with strings. (Because I have provided only string ranges example initially -- VS) That is addressed, hopefully, with the new set of examples. > Besides that, using `cover?` behavior for \[string\] ranges would introduce incompatibility. I don't know how to estimate amount of incompatibilities introduced by this behavior change. Yet it is really hard (for me) to invent some reasonable real-life use case which could be broken by it. -- https://bugs.ruby-lang.org/ Unsubscribe: