From: "sawa (Tsuyoshi Sawada)" Date: 2022-10-24T05:42:38+00:00 Subject: [ruby-core:110494] [Ruby master Bug#19080] String Range inclusion using `===` broken Issue #19080 has been updated by sawa (Tsuyoshi Sawada). You need to zero-pad the strings in order to compare them correctly by the number they express. It is and advanced thing, and people without appropriate knowledge would/should not try doing it. ---------------------------------------- Bug #19080: String Range inclusion using `===` broken https://bugs.ruby-lang.org/issues/19080#change-99815 * Author: baweaver (Brandon Weaver) * Status: Open * Priority: Normal * ruby -v: 3.1.0 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- 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: ```ruby 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. -- https://bugs.ruby-lang.org/ Unsubscribe: