Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
andrykonchin opened this issue Feb 12, 2025 · 3 comments · May be fixed by #142
Open

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

andrykonchin opened this issue Feb 12, 2025 · 3 comments · May be fixed by #142

Comments

@andrykonchin
Copy link
Member

andrykonchin commented Feb 12, 2025

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.

@kou
Copy link
Member

kou commented Feb 13, 2025

Good catch!

We should clear named captures.

@kou
Copy link
Member

kou commented Feb 20, 2025

@naitoh Do you want to work on this too?

@naitoh
Copy link
Contributor

naitoh commented Feb 20, 2025

Do you want to work on this too?

Yes, I would like to try this issue, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants