-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 Regex::MatchData#captures, named_captures, to_a, to_h #3783
Add Regex::MatchData#captures, named_captures, to_a, to_h #3783
Conversation
Ping? Any chances on getting it merged before next release? 🎉 |
`to_a` and `to_h` are just converting methods, so they follow such semantics: `self[idx] == self.to_a[idx]` and `self[idx] == self.to_h[idx] && self[key] == self.to_h[key]`. But `captures` and `named_captures` are not. `captures` contains only unnamed captures, and `named_captures` contains only named captures. Those methods looks like Ruby.
c760efe
to
635545b
Compare
Thanks @Sija. I rebased this branch to resolve conflict for the first step to merge. |
@crystal-lang lets get this merged, please :) |
@asterite Is there anything blocking this PR from merging? These are useful additions (as a plus bringing more feature-parity with Ruby). |
@asterite ping? |
Hi, is there anything preventing this PR from being merged? Almost half-year passed since its inception with completely no response from the core team... Bad PR karma :/ |
I didn't intend this, however it was counter PR against #3653. So I should explain pros/cons of this PR. Pros:
Cons:
|
Hey, sorry for the delay. What's preventing this from getting merged is that we need to compare it with #3653 and review the implementation to ensure it doesn't introduce performance issues, it's well documented etc. |
@mverzilli What did you find an issue? |
Bump @mverzilli @asterite |
|
@asterite Wouldn't |
@Sija No: # Ruby
m = "foo".match(/(f)(x)?/)
p m.captures # => ["f", nil]
p m.to_a # => ["f", "f", nil]
m = "foo".match(/(f)(?<name>x)?/)
p m.named_captures # => {"name"=>nil}
p m.to_a # => ["f", nil] Optional groups are in the MatchData as m = "foo".match(/(f)(x)?/).not_nil!
p m.captures
p m.to_a
m = "foo".match(/(f)(?<name>x)?/).not_nil!
p m.named_captures
p m.to_a Both I'll probably implement this myself soon, don't worry :-) |
@asterite I see. I didn't consider this case. My note PC is in my house today. I'd like you to implement it for fast merge. Thank you! |
@asterite Of course, totally forgot about optional groups... thanks for reminder! :) Regarding this PR vs the other one, personally I prefer this one due to familiar API (Ruby), just my 2¢. |
@makenowjust OK, I'll wait. Take your time, there's no rush at all. Thank you! |
@makenowjust Excellent, thank you! 🙇 |
to_a
andto_h
are just converting methods, so they follow such semantics:self[idx] == self.to_a[idx]
andself[idx] == self.to_h[idx] && self[key] == self.to_h[key]
.But
captures
andnamed_captures
are not.captures
contains only unnamed captures, andnamed_captures
contains only named captures. Those methods look like Ruby.