[前][次][番号順一覧][スレッド一覧]

ruby-changes:70851

From: Akira <ko1@a...>
Date: Thu, 13 Jan 2022 18:15:28 +0900 (JST)
Subject: [ruby-changes:70851] 9828502570 (master): [rubygems/rubygems] Let Version#spaceship accept a String

https://git.ruby-lang.org/ruby.git/commit/?id=9828502570

From 9828502570acce53f4094c23756bc82bd256eab7 Mon Sep 17 00:00:00 2001
From: Akira Matsuda <ronnie@d...>
Date: Tue, 11 Jan 2022 05:30:05 +0900
Subject: [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'

https://github.com/rubygems/rubygems/commit/7e0dbb79f2
---
 lib/rubygems/version.rb           | 4 +++-
 test/rubygems/test_gem_version.rb | 4 ++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/rubygems/version.rb b/lib/rubygems/version.rb
index 42e0f235053..9a26f721d60 100644
--- a/lib/rubygems/version.rb
+++ b/lib/rubygems/version.rb
@@ -341,9 +341,11 @@ class Gem::Version https://github.com/ruby/ruby/blob/trunk/lib/rubygems/version.rb#L341
   # 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
 
diff --git a/test/rubygems/test_gem_version.rb b/test/rubygems/test_gem_version.rb
index 422e1ee86c1..f8066ecbb8c 100644
--- a/test/rubygems/test_gem_version.rb
+++ b/test/rubygems/test_gem_version.rb
@@ -154,6 +154,10 @@ class TestGemVersion < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_version.rb#L154
     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
 
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]