-
-
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
Work String#starts_with? with regex #5485
Work String#starts_with? with regex #5485
Conversation
This feature is imported from Ruby 2.5.0. See: https://bugs.ruby-lang.org/issues/13712
What's the benefit over specifying "foobar" =~ /^foo/ |
Readability. |
@@ -3969,6 +3969,10 @@ class String | |||
false | |||
end | |||
|
|||
def starts_with?(re : Regex) | |||
!!($~ = re.match_at_byte_index(self, 0, Regex::Options::ANCHORED)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a fairly confusing definition, how about:
def starts_with?(re : Regex)
match_data = $~ = re.match_at_byte_index(self, 0, Regex::Options::ANCHORED)
match_data != nil
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your code has another confusability. It seems match_data
is redundant and last statement can be replaced with $~ != nil
, unfortunately it cannot. It is hard to explain this reason, so I cannot accept your suggestion. Please approve.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just think that this way is easier to understand. Perhaps:
match_data = re.match_at_byte_index(self, 0, Regex::Options::ANCHORED)
$~ = match_data
match_data != nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is such a small method that I think the original code is fine.
IMHO this is redundant and we'd expect My vote is "not fond but do whatever". |
|
I think the asymmetry with |
For example, there is a method requiring a prefix: def on_prefix(prefix)
if gets.starts_with? prefix
# process on given prefix is here.
end
end To specify two or more prefixes to this, we have to fix this method. However we don't need to fix when And now, I fixed |
I agree with the use case. But the implementation of I don't know, maybe it's not that bad just having |
IMO It is really hard to implement |
@@ -3991,6 +3995,10 @@ class String | |||
true | |||
end | |||
|
|||
def ends_with?(re : Regex) | |||
!!($~ = /#{re}\z/.match(self)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this require /(?:#{re})\z/
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needless, re.to_s
yields enclosed string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, cool.
ping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do this, @asterite's own comment pointed out a good reason for this, the common confusion between ^
and \A
.
This feature is imported from Ruby 2.5.0.
See: https://bugs.ruby-lang.org/issues/13712
This PR does not implement
String#ends_with?
forRegex
because Ruby does not and it is hard to implement. You should see above link and ruby's discussion.