From: "sferik (Erik Berlin) via ruby-core" Date: 2026-10-13T02:09:42+00:00 Subject: [ruby-core:127084] [Ruby Bug#22432] Range#include? returns true for the end of an empty range of single-character Strings Issue #22432 has been reported by sferik (Erik Berlin). ---------------------------------------- Bug #22432: Range#include? returns true for the end of an empty range of single-character Strings https://bugs.ruby-lang.org/issues/22432 * Author: sferik (Erik Berlin) * Status: Open * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- A Range whose ends are both single ASCII characters reports that it includes its end even when the range is empty. ```ruby r = "z".."a" r.to_a #=> [] r.include?("a") #=> true r.member?("a") #=> true r.cover?("a") #=> false r === "a" #=> false ``` I expect `include?` and `member?` to return false here, as `cover?` does and as iterating the range implies. The same happens with digits, for example `("1".."0").include?("0")`. Only the end is affected. Other values are correctly reported as not included: ```ruby ("z".."a").include?("z") #=> false ("z".."a").include?("m") #=> false ``` `Range#include?` has a shortcut for ranges of single-character Strings, which compares the characters instead of iterating. It is in `rb_str_include_range_p` in string.c: ```c if (ISASCII(b) && ISASCII(e) && ISASCII(v)) { if (b <= v && v < e) return Qtrue; return RBOOL(!RTEST(exclusive) && v == e); } ``` The last line returns true whenever the value equals the end, without checking that the begin is not after the end. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/