From: "Eregon (Benoit Daloze) via ruby-core" Date: 2025-04-28T10:50:09+00:00 Subject: [ruby-core:121750] [Ruby Bug#21288] StringScanner#named_captures overrides matched captures with unmatched captures Issue #21288 has been updated by Eregon (Benoit Daloze). See https://github.com/ruby/strscan/issues/145 and the linked PR ---------------------------------------- Bug #21288: StringScanner#named_captures overrides matched captures with unmatched captures https://bugs.ruby-lang.org/issues/21288#change-112806 * Author: robotdana (Dana Sherson) * Status: Open * ruby -v: ruby 3.4.3 (2025-04-14 revision d0b7e5b6a0) +PRISM [x86_64-darwin23] * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- StringScanner#named_captures operates inconsistently with MatchData#named_captures when there is the same name used in multiple branches For Regexp it uses the value that was captured (useful). For StringScanner it uses the one last in source order. ```ruby require 'strscan' re = /(?value)|(?other branch)/ scanner = StringScanner.new("value") scanner.scan(re) scanner.named_captures #=> {"test" => nil} "value".match(re).named_captures # => {"test" => "value"} scanner = StringScanner.new('other branch') scanner.scan(re) scanner.named_captures #=> {"test" => "other branch"} "other branch".match(re).named_captures # => {"test" => "other branch"} ``` -- 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/