Issue #21933 has been updated by tompng (tomoya ishida). Shorter reproduction code ~~~ruby /(?<a>foo)/ =~ 'bar' /(?<b>baz)/ =~ 'baz' p b # should be present, got nil with RUBY_BOX=1 ~~~ ~~~ruby /foo/ =~ 'bar' $~ /baz/ =~ 'baz' p $~ # should be MatchData, got nil with RUBY_BOX=1 ~~~ ---------------------------------------- Bug #21933: Ruby::Box: named capture local variable can become nil after non-matching lines https://bugs.ruby-lang.org/issues/21933#change-116572 * Author: katsyoshi (Katsuyoshi MATSUMOTO) * Status: Open * ruby -v: 4.0.1 * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- 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/