Given the following:
require "strscan"
scanner = StringScanner.new('foobar')
re1 = /(?<foo>foo)/
re2 = /(?<foo>bar)/
re = Regexp.union([re1, re2])
m = scanner.scan(re)
p m
p scanner.captures
p scanner.named_captures
p 'foobar'.match(re).named_captures
It will output:
foo"
["foo", nil]
{"foo" => nil}
{"foo" => "foo"}
Removing re2 from the union will have the scanner named captures as {"foo" => "foo"} as expected.
edit: Using re = /#{re1}|#{re2}/ also seems to have this behavior.
ruby version: ruby 3.4.2