
Issue #21040 has been updated by Eregon (Benoit Daloze). Status changed from Open to Rejected `$&` returns a new String on every usage, so this is fully expected: ``` $ ruby -e '"a" =~ /a/; p $&.object_id; p $&.object_id' 60 80 ``` ---------------------------------------- Bug #21040: String#next! method does not mutate $& variable https://bugs.ruby-lang.org/issues/21040#change-111532 * Author: radarek (Radosław Bułat) * Status: Rejected * ruby -v: ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin24] * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- `String#next!` method should mutate string and return mutated version. For some reason, using it on `$&` does not mutate it but still returns new version. Steps to reproduce (2 different ways): ```ruby "123".gsub(/./){$&.next!} # returns "234" as expected "123".gsub(/./){$&.next!;$&} # returns "123" but should be "234" "123".gsub(/./){_1.next!;_1} # returns "234" as expected "123"[/./] puts $&. # prints 1 puts $&.next! # prints 2 puts $& # prints 1 but should be 2 ``` -- https://bugs.ruby-lang.org/