I noticed strange behaviour - #named_captures may return capture groups and #[] returns nil instead of raising IndexError in the following scenario:
- call a scanning method with a Regexp (with named capture groups)
- call a scanning method without a Regexp pattern (
#getch, #get_byte or #scan_until/#exist?/... with a String pattern)
- call
#[] with a named capture group (used in the Regexp) and #named_captures
I would expect that #[] raises IndexError and #named_captures returns nil.
Example
require 'strscan'
s = StringScanner.new('Fri Dec 12 1975 14:39')
s.scan(/(?<wday>Fri)/)
p s.named_captures # => {"wday" => "Fri"}
p s["wday"] # => "Fri"
s.get_byte
p s.named_captures # => {"wday" => nil}
p s["wday"] # => nil
s["month"] # => undefined group name reference: month (IndexError)
Wondering whether it's correct behaviour.