Add #to_s method to VERSION modules #8501

Merged
merged 3 commits into from Mar 24, 2013

10 participants

@charliesome

IMO this is pretty useless behaviour:

>> Rails::VERSION
=> Rails::VERSION
>> ActiveSupport::VERSION
=> ActiveSupport::VERSION

This patch adds a #to_s method so that accessing *::VERSION gives useful output in REPLs, etc:

>> Rails::VERSION
=> 4.0.0.beta
>> ActiveSupport::VERSION
=> 4.0.0.beta
@jeremy
Ruby on Rails member

Good thinking!

Following that line of thinking, why do our own VERSION modules? Would be nice if...

>> ActiveSupport::VERSION
=> #<Gem::Version "3.2.9">
@steveklabnik
Ruby on Rails member

@jeremy 👍

@steveklabnik
Ruby on Rails member

@charliesome interested in changing the implementation to do that?

@charliesome

Although this could potentially break other things which expect VERSION to be a module with the right constants on it. I don't think this is ideal seeing as VERSION should definitely be one of the things which maintains backwards compatibility

@steveklabnik
Ruby on Rails member

I'm not sure, exactly. I bet you could manage both, just because ActiveSupport::Version is now a Gem::Version doesn't mean you can't make those old constants inside of it as well.

@charliesome

That might prove difficult seeing as you can only set constants on classes and modules

@jeremy
Ruby on Rails member
diff --git a/activesupport/lib/active_support/version.rb b/activesupport/lib/active_support/version.
index 8a8f8f9..5c32038 100644
--- a/activesupport/lib/active_support/version.rb
+++ b/activesupport/lib/active_support/version.rb
@@ -1,10 +1,9 @@
 module ActiveSupport
-  module VERSION #:nodoc:
-    MAJOR = 4
-    MINOR = 0
-    TINY  = 0
-    PRE   = "beta"
+  class << self; attr_reader :version; end
+  @version = Gem::Version.new('4.0.0.beta')

-    STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
+  module VERSION #:nodoc:
+    MAJOR, MINOR, TINY, PRE = ActiveSupport.version.segments
+    STRING = ActiveSupport.version.to_s
   end
 end

c/d?

@charliesome

Oh right, I misunderstood what @steveklabnik meant.

@jeremy 👍

@steveklabnik
Ruby on Rails member

Yep. 👍

@senny
Ruby on Rails member

@charliesome will you update the PR to make use of Gem::Version?

@charliesome
@charliesome

@senny @steveklabnik @jeremy can you guys check the latest commits?

The only incompatibility I have introduced is that Rails.version now returns a Gem::Version where it used to return a String. Personally I think this is ok, but I'm happy to hear your thoughts.

@steveklabnik
Ruby on Rails member

Seems legit. This doesn't merge cleanly though.

@charliesome

@steveklabnik whoops! rebased against latest master

@rafaelfranca
Ruby on Rails member

@charliesome need a changelog entry for the Rails.version change

@rafaelfranca rafaelfranca merged commit e600384 into rails:master Mar 24, 2013
@robertomiranda robertomiranda referenced this pull request in rails/jquery-rails Mar 24, 2013
Merged

Add #to_s method to rails version #117

@charliesome charliesome deleted the charliesome:version-to-s branch Mar 24, 2013
@adammathys adammathys referenced this pull request in DavyJonesLocker/client_side_validations Mar 25, 2013
Closed

Add to_s for Rails.version #510

@adammathys adammathys pushed a commit to StemboltHQ/carmen-rails that referenced this pull request Mar 25, 2013
Adam Mueller Add to_s to Rails.version
Due to rails/rails#8501.

Change-Id: I3a5580e12a9f4bc013bb98db9aa6d8c3e11bf0c5
92d6d10
@vwall vwall added a commit to vwall/rspec-rails that referenced this pull request Mar 25, 2013
@vwall vwall Add #to_s method to rails version
Rails.version returns an instance of Gem::Version
ref rails/rails#8501, https://travis-ci.org/rails/rails/jobs/5750326
37e78bf
@tamird

All ye who enter here, if you are checking rails versions:

2.0.0p0 :020 > '4.0.0.beta1' > '4.0.0.1'
 => true
2.0.0p0 :021 > Gem::Version.new('4.0.0.beta1') > Gem::Version.new('4.0.0.1')
 => false

String comparison semantics are different from Gem::Version comparison semantics.

Also:

2.0.0p0 :004 > Gem::Requirement.new('>= 3.0').satisfied_by?(Rails.version)
 => true
@elabs-dev elabs-dev pushed a commit to jnicklas/capybara that referenced this pull request Mar 26, 2013
Kim Burgestrand Make sure we are comparing gem versions, closes #1024
Due to rails/rails#8501, we can no longer case the Rails version to a float. This version should hopefully work on both Rails 4 master and on Rails 3.
78e3a48
@jnicklas jnicklas added a commit to jnicklas/capybara that referenced this pull request Mar 26, 2013
Kim Burgestrand Make sure we are comparing gem versions, closes #1024
Due to rails/rails#8501, we can no longer case the Rails version to a float. This version should hopefully work on both Rails 4 master and on Rails 3.
6d0a7da
@bai

This breaks so many things 👎

@bai bai referenced this pull request in tsechingho/chosen-rails Mar 27, 2013
Merged

Add #to_s method to VERSION modules #22

@jnicklas

I agree with @bai. I appreciate that this is a good change, and that comparing strings isn't a good idea. But a lot of people are doing it anyway, and a lot of stuff breaks due to this change. I moved a project from beta1 to master and there were several gems who broke due to this.

I understand if you want to introduce this change anyway, it's probably for the best, I just want to make sure you guys are aware of how disruptive this is.

@charliesome
@rafaelfranca
Ruby on Rails member

@charliesome 👍 for this path. Mind to you open a pull request?

@jeremy
Ruby on Rails member

👍

@steveklabnik
Ruby on Rails member

It's also a trivial thing to update; I've already fixed it in all my gems. :)

Then again, other people haven't, so whatever.

@charliesome
@crazymykl

I think Gem::Version comparison semantics are a lot more sensible than those for strings. 1.0final < 1.0rc3?

Although this is a minorly breaking change, it breaks things which are actually wrong semantically, so I'm against reverting it.

@jnicklas

I agree with you guys. I think we should keep it. It's just a good thing to keep in mind that it'll probably break a lot of stuff.

@charliesome charliesome added a commit to charliesome/rails that referenced this pull request Mar 30, 2013
@charliesome charliesome reverts changes to Rails.version made in #8501 ceb3b87
@zolzaya

👍

@arthurnn arthurnn added a commit to arthurnn/rpm that referenced this pull request Apr 1, 2013
@arthurnn arthurnn Rails.version must force to_s after rails 4
Rails.version now return a Gem::Version instance. If we want a string
from it, we should call .to_s method. this is also backwards compatible
[ref rails/rails#8501]
d35dc32
@sikachu sikachu added a commit to sikachu/rails that referenced this pull request Feb 18, 2014
@sikachu sikachu Introduce `Rails.gem_version`
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`.
37cf1f4
@sikachu sikachu added a commit to sikachu/rails that referenced this pull request Feb 18, 2014
@sikachu sikachu Introduce `Rails.gem_version`
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`.
38b9073
@sikachu sikachu added a commit to sikachu/rails that referenced this pull request Feb 24, 2014
@sikachu sikachu Introduce `Rails.gem_version`
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`.
80c07e0
@sikachu sikachu added a commit to sikachu/rails that referenced this pull request Feb 24, 2014
@sikachu sikachu Introduce `Rails.gem_version`
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`.
6cfdd4c
@sikachu sikachu added a commit to sikachu/rails that referenced this pull request Feb 24, 2014
@sikachu sikachu Introduce `Rails.gem_version`
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`.
f39616e
@sikachu sikachu added a commit to sikachu/rails that referenced this pull request Feb 24, 2014
@sikachu sikachu Introduce `Rails.gem_version`
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`.
0e8cdc7
@sikachu sikachu added a commit to sikachu/rails that referenced this pull request Feb 28, 2014
@sikachu sikachu Introduce `Rails.gem_version`
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`.
7388232
@sikachu sikachu added a commit to sikachu/rails that referenced this pull request Feb 28, 2014
@sikachu sikachu Introduce `Rails.gem_version`
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`.
c17b9e8
@sikachu sikachu added a commit to sikachu/rails that referenced this pull request Mar 5, 2014
@sikachu sikachu Introduce `Rails.gem_version`
This method return `Gem::Version.new(Rails.version)`, suggesting 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 #8501
but got reverted in #10002 since it was not backward compatible.

Also, updating template for `rake update_versions`.
54ed1f1
@sikachu sikachu added a commit to sikachu/rails that referenced this pull request Mar 5, 2014
@sikachu sikachu Introduce `Rails.gem_version`
This method return `Gem::Version.new(Rails.version)`, suggesting 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 #8501
but got reverted in #10002 since it was not backward compatible.

Also, updating template for `rake update_versions`.
5bf8edd
@sikachu sikachu added a commit to sikachu/rails that referenced this pull request Mar 5, 2014
@sikachu sikachu Introduce `Rails.gem_version`
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 #8501
but got reverted in #10002 since it was not backward compatible.

Also, updating template for `rake update_versions`.
2dd2fcf
@sikachu sikachu added a commit to sikachu/rails that referenced this pull request Mar 5, 2014
@sikachu sikachu Introduce `Rails.gem_version`
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 #8501
but got reverted in #10002 since it was not backward compatible.

Also, updating template for `rake update_versions`.
3bf87d7
@ttosch ttosch pushed a commit that referenced this pull request Jan 19, 2015
@sikachu sikachu Introduce `Rails.gem_version`
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 #8501
but got reverted in #10002 since it was not backward compatible.

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