
Issue #20421 has been updated by byroot (Jean Boussier). I got a ruby-bug spec at https://github.com/ruby/ruby/pull/10750 As for 9dcdffb8bf8a3654fd78bf1a58b30c8e13888a7a, it might make sense to make a more targeted fix for backport, as this is a more general refactor. ---------------------------------------- Bug #20421: String#index and String#byteindex don't clear `$~` when offset > size (or bytesize) https://bugs.ruby-lang.org/issues/20421#change-108225 * Author: andrykonchin (Andrew Konchin) * Status: Closed * ruby -v: 3.2.2 * Backport: 3.1: REQUIRED, 3.2: REQUIRED, 3.3: DONTNEED ---------------------------------------- When String#index and String#byteindex are called with Regexp and offset they clear `$~` (that contains result of the last Regexp matching) when offset < -size. But they don't clear `$~` when offset > size what seems inconsistent: ```ruby "a".index(/a/) p $~ # => #<MatchData "a"> string = "blablabla" string.index(/bla/, string.length + 1) p $~ # => #<MatchData "a"> ``` -- https://bugs.ruby-lang.org/