[ruby-core:121747] [Ruby Bug#21288] StringScanner#named_captures overrides matched captures with unmatched captures
From:
"robotdana (Dana Sherson) via ruby-core" <ruby-core@...>
Date:
2025-04-28 01:55:46 UTC
List:
ruby-core #121747
Issue #21288 has been reported by robotdana (Dana Sherson).
----------------------------------------
Bug #21288: StringScanner#named_captures overrides matched captures with unmatched captures
https://bugs.ruby-lang.org/issues/21288
* Author: robotdana (Dana Sherson)
* Status: Open
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
StringScanner#named_captures operates inconsistently with Regexp#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" => "test"}
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/