Project

General

Profile

Actions

Bug #19080

closed

String Range inclusion using `===` broken

Added by baweaver (Brandon Weaver) about 2 years ago. Updated about 2 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:110492]

Description

When using === implicitly for a pattern match I noticed a behavior which I believe is a bug in Ruby. === does not match with include? for ranges of String types, but also does so inconsistently:

range = '0'..'255'

('1'..'5').map do |v|
  {
    v: v,
    teq: range === v,
    include?: range.include?(v)
  }
end
#  => [
#   { v: "1", teq: true, include?: true },
#   { v: "2", teq: true, include?: true },
#   { v: "3", teq: false, include?: true },
#   { v: "4", teq: false, include?: true },
#   { v: "5", teq: false, include?: true }
# ]

# But numbers work??
range = 0..255

(1..5).map do |v|
  {
    v: v,
    teq: range === v,
    include?: range.include?(v)
  }
end
# => [
#   { v: 1, teq: true, include?: true},
#   { v: 2, teq: true, include?: true},
#   { v: 3, teq: true, include?: true},
#   { v: 4, teq: true, include?: true},
#   { v: 5, teq: true, include?: true}
# ]

Am I doing something wrong? This does not feel like it is working as expected. I might dig through the C code to see how this is implemented and why this happens, but am currently confused by it.


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #14575: Switch Range#=== to use cover? instead of include?Closednobu (Nobuyoshi Nakada)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0