-
Notifications
You must be signed in to change notification settings - Fork 194
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
Rubocop: Style/SafeNavigation #1518
Conversation
6b20d2a
to
6b6b211
Compare
@@ -26,8 +26,7 @@ def pad_if_necessary(hex) | |||
# Returns a 3- or 6-char hex string for valid input, or nil | |||
# for invalid input. | |||
def validate(hex) | |||
match = /^#?(([0-9a-f]{3}){1,2})$/i.match(hex) | |||
match && match[1] | |||
/^#?(([0-9a-f]{3}){1,2})$/i.match(hex)&.[](1) |
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 the only line of this PR I don't like - explicitly calling []
seems weird and perhaps unclear that it's equivalent
Alternatively, we could disable this cop on this line
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.
Agreed, that makes an already dense snippet even more obscure.
What's the cop say if we try to use a post-fix if instead?
match = /^#?(([0-9a-f]{3}){1,2})$/i.match(hex)
match[1] if match
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.
Could also move the regex into a constant imo :)
however yes, &.[](1)
doesn't seem nice
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.
Agreed with the both of you :)
@@ -26,8 +26,7 @@ def pad_if_necessary(hex) | |||
# Returns a 3- or 6-char hex string for valid input, or nil | |||
# for invalid input. | |||
def validate(hex) | |||
match = /^#?(([0-9a-f]{3}){1,2})$/i.match(hex) | |||
match && match[1] | |||
/^#?(([0-9a-f]{3}){1,2})$/i.match(hex)&.[](1) |
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.
Could also move the regex into a constant imo :)
however yes, &.[](1)
doesn't seem nice
6b6b211
to
4b608c3
Compare
Rubocop still complained on the Post-fix - updated with the regex pulled out into a constant and the cop disabled inline. |
Merge conflict now and soemhow a test failed but travis doesn't load for me |
4b608c3
to
87d67f4
Compare
Looking 🍏 now. Thanks! |
No description provided.