From: zverok.offline@... Date: 2016-11-25T09:11:47+00:00 Subject: [ruby-core:78340] [Ruby trunk Feature#12612] Switch Range#=== to use cover? instead of include? Issue #12612 has been updated by Victor Shepelev. I encountered the problem in production code, it was not strings, but specialized IPAddress class, to filter "if IP is in range" -- it was SUDDENLY 0.4 sec on each request to server spent just to `(IPAddress.new(from)..IPAddress.new(to)).to_a` in "innocently" looking: ~~~ruby case ip when admin_ip_range ... ~~~ Another real cases: * ranges of Dates/Times/DateTimes * Measure units (like https://github.com/joshwlewis/unitwise) * Any other specialized _enumerable_ AND _comparable_ value class Yes, proposed behavior is incompatible with current, but I am not sure it is a bad thing: ~~~ruby (Date.parse('2016-05-01')..Date.today) === DateTime.parse('2016-06-01 12:30') # => false (Date.parse('2016-05-01')..Date.today).cover?(DateTime.parse('2016-06-01 12:30')) # => true # Which is more logical? What you'll intuitively expect here: case DateTime.parse('2016-06-01 12:30') when Date.parse('2016-05-01')..Date.today # looks like we SHOULD be here, but... ~~~ ---------------------------------------- Feature #12612: Switch Range#=== to use cover? instead of include? https://bugs.ruby-lang.org/issues/12612#change-61706 * Author: Victor Shepelev * Status: Rejected * Priority: Normal * Assignee: ---------------------------------------- Currently, `Range#===` is an alias of `#include?`, which works by `#each`-ing all the values and comparing them to the checked value. It may lead to fascinating inefficiencies, when working with strings or other similar classes (for example, IP class for allowing/disallowing IP ranges). Moreover, it is not consistent with behavior of numerical ranges (which is kinda special case, I guess): ```ruby (1...3) === 2.5 # true - though (1..3).to_a.include?(2.5) is false ('a'...'z') === 'foo' # false - because ('a'..'z').to_a.include?('foo') is false ``` As `===` is heavily used in `case` and `grep`, and there is no option to replace it manually with `cover?` there, maybe it would be reasonable to change the behavior? To be honest, I could imagine no real cases when `include?` is preferable for identity check. -- https://bugs.ruby-lang.org/ Unsubscribe: