Actions
Bug #21288
closedStringScanner#named_captures overrides matched captures with unmatched captures
Description
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.
require 'strscan'
re = /(?<test>value)|(?<test>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"}
Updated by robotdana (Dana Sherson) 5 days ago
- ruby -v set to ruby 3.4.3 (2025-04-14 revision d0b7e5b6a0) +PRISM [x86_64-darwin23]
Updated by Eregon (Benoit Daloze) 4 days ago
See https://github.com/ruby/strscan/issues/145 and the linked PR
Updated by kou (Kouhei Sutou) 4 days ago
- Status changed from Open to Closed
- Assignee set to kou (Kouhei Sutou)
strscan 3.1.4 gem fixed this.
Actions
Like0
Like0Like0Like0Like0Like0