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

Add String#match! #13119

Closed
devnote-dev opened this issue Feb 26, 2023 · 0 comments · Fixed by #13285
Closed

Add String#match! #13119

devnote-dev opened this issue Feb 26, 2023 · 0 comments · Fixed by #13285

Comments

@devnote-dev
Copy link
Contributor

This would be a non-nilable version of String#match. A lot of the standard library has methods for something that can error and something that should return nil instead, for example, the #[] and #[]? convention which is used throughout the standard library. This method does not, and requires doing one of the following for usage:

match = "Crystal".match(/(cr)/i).not_nil![1]

"Crystal".match(/(cr)/i)
match = $1

While you could use $1 to get around this, it becomes a pain to manage when you have a large number of capture groups or a recursive capture group, and it's not easy to identify which $ variable represents what capture. Ideally, String#match? would be preferable here, but String#match can return nil and to change that to non-nilable would be a breaking change. It's a slight inconsistency, but it's better than nothing.

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

Successfully merging a pull request may close this issue.

2 participants