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

Support endless ranges in where #34906

Merged

Conversation

gregnavis
Copy link
Contributor

@gregnavis gregnavis commented Jan 9, 2019

Summary

This commit adds support for endless ranges, e.g. (1..), that were added in Ruby 2.6. They're functionally equivalent to explicitly specifying Float::INFINITY as the end of the range.

Specifically:

relation.where(column: 1..Float::INFINITY)

is equivalent to

relation.where(column: (1..))

Other Information

Only the end of a range can be omitted. Trying (..1) results in a syntax error so there's no need to add support for implicit -Float::INFINITY.

Alternative Implementation

We could modify other at the beginning of between and replace nil with Float::INFINITY (or even support -Float::INFINITY) so that there'd be no changes to conditions in if statements below + we would be able to support nil..1 (equivalent to -Float::INFINITY..1).

Please let me know if you find this preferable and I'll update the PR.

@@ -639,6 +639,16 @@ class AttributeTest < Arel::Spec
)
end

it "can be constructed with a range implicitly ending at Infinity" do
attribute = Attribute.new nil, nil
node = attribute.between(0..)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will give us a syntax error on 2.5. Can you conditionalize this test so we don't get build failures on 2.5?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. The built actually failed for precisely this reason. I'll push an update tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tenderlove just pushed an update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'oh, what I did won't work for syntax error 🙈

I can either:

  1. Come up with something that would work for syntax errors.
  2. Leave the check as it is and replace 0.. with 0..nil (they're equivalent). In Rubyies pre-2.6 the latter results in an ArgumentError exception.

@tenderlove what do you think?

@gregnavis gregnavis force-pushed the add-endless-ranges-to-activerecord branch from 3a67c12 to 20b108a Compare January 10, 2019 13:57
if Gem::Version.new('2.6.0') <= Gem::Version.new(RUBY_VERSION)
it "can be constructed with a range implicitly ending at Infinity" do
attribute = Attribute.new nil, nil
node = attribute.between(0..)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I suggest this?

Suggested change
node = attribute.between(0..)
node = attribute.between(eval("0..")) # Use eval for compatibility with Ruby < 2.6 parser.

This way Ruby parser in version < 2.6 will just overlook it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea. Thanks!

@gregnavis gregnavis force-pushed the add-endless-ranges-to-activerecord branch 2 times, most recently from 905d11e to fbccf87 Compare January 11, 2019 10:19
Copy link
Member

@sikachu sikachu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind checking CodeClimate? Looks like in the test, line 642 and 645 should both use double quote and not single quote.

This commit adds support for endless ranges, e.g. (1..), that were added
in Ruby 2.6. They're functionally equivalent to explicitly specifying
Float::INFINITY as the end of the range.
@gregnavis gregnavis force-pushed the add-endless-ranges-to-activerecord branch from fbccf87 to 7110dbe Compare January 11, 2019 13:27
@gregnavis
Copy link
Contributor Author

@tenderlove @sikachu thanks for comments! The build is green now. Please have another look.

Copy link
Member

@sikachu sikachu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good to merge. @tenderlove, please!

@tenderlove tenderlove merged commit 3a1b2a2 into rails:master Jan 11, 2019
kamipo added a commit that referenced this pull request Jan 11, 2019
And support endless ranges for `not_between` like as `between`.

Follow up #34906.
suketa added a commit to suketa/rails_sandbox that referenced this pull request Jul 14, 2019
Support endless ranges in where
rails/rails#34906
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants