Skip to content

Scanning methods that don't use Regexp don't clear named capture groups #135

@andrykonchin

Description

@andrykonchin

I noticed strange behaviour - #named_captures may return capture groups and #[] returns nil instead of raising IndexError in the following scenario:

  1. call a scanning method with a Regexp (with named capture groups)
  2. call a scanning method without a Regexp pattern (#getch, #get_byte or #scan_until/#exist?/... with a String pattern)
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions