Project

General

Profile

Actions

Bug #21288

closed

StringScanner#named_captures overrides matched captures with unmatched captures

Added by robotdana (Dana Sherson) 5 days ago. Updated 4 days ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 3.4.3 (2025-04-14 revision d0b7e5b6a0) +PRISM [x86_64-darwin23]
[ruby-core:121747]

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"}
Actions #1

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]
Actions #2

Updated by robotdana (Dana Sherson) 5 days ago

  • Description updated (diff)
Actions #3

Updated by robotdana (Dana Sherson) 4 days ago

  • Description updated (diff)

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

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0