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
Fix beginless and endless range comparisons #51159
base: main
Are you sure you want to change the base?
Fix beginless and endless range comparisons #51159
Conversation
23c8fae
to
15d9de3
Compare
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.
Both methods have this comment which should be changed:
# The given range must be fully bounded, with both start and end.
Thinking out loud: It seems like include?
ends up very similar if not the same as cover?
, are there any differences left?
Edit: introduced in 99c6482, I wonder if we should just recommend people use cover?
now 🤔
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.
@AlexMooney i've looked at the code over and over again and i think it could be written in a clearer way. a lot of things get in the way of comprehension. i recommend that you separate the endless/beginless range concern, and handle with a private method.
Thanks for catching that! I've removed it.
I don't think there are any differences in numerical values, but in non-numeric ranges you can get different answers: > ('a'..'d').=== 'cc'
true
> ('a'..'d').include? 'cc'
false
The reason I ended up here is this extension messing with ranges makes some spec like
@yawboakye I've rewritten it to use smaller private methods so that I could name the concepts involved. Let me know if you like this way better. I'll squish the PR back into 1 commit after the code looks acceptable. |
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.
one more (and final) refactoring. from where i sit, both ===
and include?
seem to have identical implementations, so i recommend we make one the alias of the other.
That is almost but not entirely so. The |
good point 👍! we can still move the range check condition outside of |
I've separated the |
looks good to me cc @skipkayhil |
Some range comparisons raise `RangeError`, for example: ```ruby > (1..) === (2..) RangeError: cannot get the last element of endless range from …/activesupport-7.1.3/lib/active_support/core_ext/range/compare_range.rb:23:in `last' ```
6c79a33
to
94a3404
Compare
@skipkayhil can I get another review, please? |
@skipkayhil nudge to take a look at the PR again, please. |
Motivation / Background
This Pull Request has been created because some endless and beginless range comparisons raise
RangeError
due toActiveSupport::CompareWithRange
Detail
This Pull Request changes the
CompareWithRange#===
andCompareWithRange#include?
methods to support begin and endless ranges on the left and right hand side of the comparison.Additional information
POR:
Rails 7.1.3:
This PR:
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]