[ruby-core:121758] [Ruby Bug#21288] StringScanner#named_captures overrides matched captures with unmatched captures
From:
"kou (Kouhei Sutou) via ruby-core" <ruby-core@...>
Date:
2025-04-28 22:21:36 UTC
List:
ruby-core #121758
Issue #21288 has been updated by kou (Kouhei Sutou).
Status changed from Open to Closed
Assignee set to kou (Kouhei Sutou)
strscan 3.1.4 gem fixed this.
----------------------------------------
Bug #21288: StringScanner#named_captures overrides matched captures with unmatched captures
https://bugs.ruby-lang.org/issues/21288#change-112815
* Author: robotdana (Dana Sherson)
* Status: Closed
* Assignee: kou (Kouhei Sutou)
* 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 = /(?<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"}
```
--
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/