Skip to content

Introduce Rails.gem_version#14101

Merged
jeremy merged 1 commit into
rails:masterfrom
sikachu:master-fix-versioning-task
Mar 5, 2014
Merged

Introduce Rails.gem_version#14101
jeremy merged 1 commit into
rails:masterfrom
sikachu:master-fix-versioning-task

Conversation

@sikachu

@sikachu sikachu commented Feb 18, 2014

Copy link
Copy Markdown
Member

This method returns a Gem::Version object, which is useful when performing a comparison between versions. This was originally introduced as .version by @charliesome in #8501, but got reverted in
#10002 since it was not backward compatible.

Also, updating template for rake update_versions.

@guilleiguaran

Copy link
Copy Markdown
Member

/cc @jeremy

@robin850

Copy link
Copy Markdown
Member

Excuse me if I'm wrong but existing libraries are relying on #version to ensure compatibility between several versions of Rails, no ?

@sikachu

sikachu commented Feb 24, 2014

Copy link
Copy Markdown
Member Author

@robin850 Yes, and that's why #8501 was reverted. I've stated that already in the PR description.

That's why I'm introducing .gem_version. Going forward, we will want people to rely on .gem_version to do the comparison.

Note that Rails.version is still there, as a string, after this commit.

@robin850

Copy link
Copy Markdown
Member

Note that Rails.version is still there, as a string, after this commit.

Ah sorry, missed that part. Thanks for the explanation.

@jeremy

jeremy commented Feb 24, 2014

Copy link
Copy Markdown
Member

This also changes each framework's #version to a string, breaking compatibility. Consider introducing Rails.gem_version only and leaving the others as-is.

@sikachu

sikachu commented Feb 25, 2014

Copy link
Copy Markdown
Member Author

@jeremy Then what should we do with partially-introduced #version of each framework's method? Just left them as is, or remove them? I thought they were introduced by mistake?

Comment thread railties/CHANGELOG.md Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Introduces -> Introduce
returns -> return

We say it's useful for performing version comparison. Let's show an example. A good one is how Rails.version > '4.1.10' would be true for '4.1.2' also due to lexicographic string comparison, but would be false when comparing gem versions.

Unfortunately you can't do Rails.gem_version > '4.1.10'. You have to:

if Rails.gem_version > Gem::Version.new('4.1.10')
  # ...

and that's limited to exact version comparison. Pretty verbose and hard to discover.

Furthermore, to do more realistic version requirement checks, you'd use:

if Gem::Requirement.create('~> 4.1.2') =~ Rails.gem_version
  ...

Which you'd need some digging in RubyGems docs to even figure out.

All told, we may be better off just saying: "Introduce Rails.gem_version as a convenience method to return Gem::Version.new(Rails.version)".

It's not something most app developers would ever touch, so suggesting it's useful is a stretch, but plugin developers may take note and use it.

@sikachu

sikachu commented Mar 5, 2014

Copy link
Copy Markdown
Member Author

@jeremy thanks for your awesome feedbacks. I've updated the code on this and #14103. How does it look now?

Comment thread railties/CHANGELOG.md Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"suggesting more" -> "suggesting a more"

This method return `Gem::Version.new(Rails.version)`, suggesting a more
reliable way to perform version comparison.

Example:

    Rails.version #=> "4.1.2"
    Rails.gem_version #=> #<Gem::Version "4.1.2">

    Rails.version > "4.1.10" #=> false
    Rails.gem_version > Gem::Version.new("4.1.10") #=> true
    Gem::Requirement.new("~> 4.1.2") =~ Rails.gem_version #=> true

This was originally introduced as `.version` by @charliesome in rails#8501
but got reverted in rails#10002 since it was not backward compatible.

Also, updating template for `rake update_versions`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants