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

MatchData#[] with an unbounded Range not supported #2755

Closed
postmodern opened this issue Oct 5, 2022 · 3 comments
Closed

MatchData#[] with an unbounded Range not supported #2755

postmodern opened this issue Oct 5, 2022 · 3 comments

Comments

@postmodern
Copy link

postmodern commented Oct 5, 2022

It does not appear that MatchData#[] supports accepting an unbounded range of indices.

Steps To Reproduce

string = "aaaabababab"
regexp = /(ab)(ab)(ab)/
match = string.match(regexp)
p match[1..]

Expected Result

["ab", "ab", "ab"]

Actual Result

<internal:core> core/type.rb:276:in `convert_type': no implicit conversion of Range into Integer (TypeError)
	from <internal:core> core/type.rb:178:in `rb_to_int_fallback'
	from (irb):4:in `[]'
	from (irb):4:in `<top (required)>'
	from <internal:core> core/kernel.rb:407:in `loop'
	from <internal:core> core/throw_catch.rb:36:in `catch'
	from <internal:core> core/throw_catch.rb:36:in `catch'
	from /opt/rubies/truffleruby-22.2.0/lib/gems/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
	from <internal:core> core/kernel.rb:376:in `load'
	from <internal:core> core/kernel.rb:376:in `load'
	from /opt/rubies/truffleruby-22.2.0/bin/irb:42:in `<main>'

Version Information

  • truffleruby 22.2.0, like ruby 3.0.3, GraalVM CE Native [x86_64-linux]
@nirvdrum
Copy link
Collaborator

nirvdrum commented Oct 5, 2022

Unfortunately, that error message isn't entirely helpful. We do support ranges, but only when both the start and end values are specified.

string = "aaaabababab"
regexp = /(ab)(ab)(ab)/
match = string.match(regexp)
p match[1..3] # => ["ab", "ab", "ab"]

There's nothing in the Ruby Spec Suite for this, so it's likely an oversight. We'll fix the issue, but if that's a blocker, you can avoid it by specifying the end value in the range, providing you have it.

@postmodern postmodern changed the title MatchData[Range] not supported MatchData#[] with an unbounded Range not supported Oct 5, 2022
@eregon
Copy link
Member

eregon commented Oct 11, 2022

As a note, there is MatchData#captures for this exact pattern (from ronin-rb/ronin-vulns@b7f472e#r86454351)

@andrykonchin
Copy link
Member

Thank you for report. Fixed in b7d7cd0.

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

No branches or pull requests

4 participants