Issue #21933 has been updated by k0kubun (Takashi Kokubun). Backport changed from 3.2: DONTNEED, 3.3: DONTNEED, 3.4: DONTNEED, 4.0: REQUIRED to 3.2: DONTNEED, 3.3: DONTNEED, 3.4: DONTNEED, 4.0: DONE ruby_4_0 commit:dd78605b2d06600750c331f307083d60df702814 merged revision(s) commit:526344b56ea968d5704bdefe6e10bb3cf7f4f569, commit:8ad6baa01746e8de0460f0ccdaee69953a70af17. ---------------------------------------- Bug #21933: Ruby::Box: named capture local variable can become nil after non-matching lines https://bugs.ruby-lang.org/issues/21933#change-117268 * Author: katsyoshi (Katsuyoshi MATSUMOTO) * Status: Closed * ruby -v: 4.0.1 * Backport: 3.2: DONTNEED, 3.3: DONTNEED, 3.4: DONTNEED, 4.0: DONE ---------------------------------------- Environment: - Ruby 4.0.1 - RUBY_BOX=1 - OS: Linux x86_64 Summary: With Ruby::Box enabled, a named capture local variable from `=~` may become nil, even when the regexp matches. This happens after iterating over non-matching lines first. Reproducer: sample.rb ``` ruby text = <<~ASM .intel_syntax noprefix .globl main main: nop ASM cur=nil text.each_line do |line| if /^(?<label>[_A-Za-z.]\w*):/ =~ line p [:matched, line.inspect, label.inspect] cur = label break end end p [:cur, cur.inspect] ``` Expected: ``` shell [:matched, "\"main:\\n\"", "\"main\""] [:cur, "\"main\""] ``` Actual (with RUBY_BOX=1): ``` shell [:matched, "\"main:\\n\"", "nil"] [:cur, "nil"] ``` Control: Without RUBY_BOX=1, the same script returns "main" as expected. -- https://bugs.ruby-lang.org/