mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[rubygems/rubygems] Let Version#spaceship accept a String
With this patch, handwriting version comparisons become a little bit easier.
before:
SomeGem.version <=> Gem::Version.new('1.3')
after:
SomeGem.version <=> '1.3'
7e0dbb79f2
This commit is contained in:
parent
9de380860d
commit
9828502570
2 changed files with 7 additions and 1 deletions
|
@ -341,9 +341,11 @@ class Gem::Version
|
|||
# Compares this version with +other+ returning -1, 0, or 1 if the
|
||||
# other version is larger, the same, or smaller than this
|
||||
# one. Attempts to compare to something that's not a
|
||||
# <tt>Gem::Version</tt> return +nil+.
|
||||
# <tt>Gem::Version</tt> or a valid version String return +nil+.
|
||||
|
||||
def <=>(other)
|
||||
return self <=> self.class.new(other) if (String === other) && self.class.correct?(other)
|
||||
|
||||
return unless Gem::Version === other
|
||||
return 0 if @version == other._version || canonical_segments == other.canonical_segments
|
||||
|
||||
|
|
|
@ -154,6 +154,10 @@ class TestGemVersion < Gem::TestCase
|
|||
assert_equal(-1, v("5.a") <=> v("5.0.0.rc2"))
|
||||
assert_equal(1, v("5.x") <=> v("5.0.0.rc2"))
|
||||
|
||||
assert_equal(0, v("1.9.3") <=> "1.9.3")
|
||||
assert_equal(1, v("1.9.3") <=> "1.9.2.99")
|
||||
assert_equal(-1, v("1.9.3") <=> "1.9.3.1")
|
||||
|
||||
assert_nil v("1.0") <=> "whatever"
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue