回答:
Gem::Version.new('0.4.1') > Gem::Version.new('0.10.1')
Gem::Version...
構文は、私が宝石をインストールする必要があるだろうと思いました。しかし、それは必須ではありませんでした。
Gem::Dependency.new(nil, '~> 1.4.5').match?(nil, '1.4.6beta4')
require 'rubygems'
アクセスするにはが必要ですGem
。ただし、1.9以降は自動的に含まれます。
悲観的なバージョンの制約を確認する必要がある場合は、次のようにGem :: Dependencyを使用できます。
Gem::Dependency.new('', '~> 1.4.5').match?('', '1.4.6beta4')
Gem::Dependency.new('', '~> 1.4.5').match?('', '1.4.6beta4')
class Version < Array
def initialize s
super(s.split('.').map { |e| e.to_i })
end
def < x
(self <=> x) < 0
end
def > x
(self <=> x) > 0
end
def == x
(self <=> x) == 0
end
end
p [Version.new('1.2') < Version.new('1.2.1')]
p [Version.new('1.2') < Version.new('1.10.1')]
vers = (1..3000000).map{|x| "0.0.#{x}"}; 'ok' puts Time.now; vers.map{|v| ComparableVersion.new(v) }.sort.first; puts Time.now # 24 seconds 2013-10-29 13:36:09 -0700 2013-10-29 13:36:33 -0700 => nil puts Time.now; vers.map{|v| Gem::Version.new(v) }.sort.first; puts Time.now # 41 seconds 2013-10-29 13:36:53 -0700 2013-10-29 13:37:34 -0700
コードblobは見苦しくなりますが、基本的に、これとGem :: Versionを使用すると、約2倍速くなります。
あなたは使用することができますVersionomy
宝石(で利用可能githubのを):
require 'versionomy'
v1 = Versionomy.parse('0.1')
v2 = Versionomy.parse('0.2.1')
v3 = Versionomy.parse('0.44')
v1 < v2 # => true
v2 < v3 # => true
v1 > v2 # => false
v2 > v3 # => false
私はするだろう
a1 = v1.split('.').map{|s|s.to_i}
a2 = v2.split('.').map{|s|s.to_i}
その後、行うことができます
a1 <=> a2
(そしておそらく他のすべての「通常の」比較)。
... <
または>
テストが必要な場合は、たとえば
(a1 <=> a2) < 0
または、必要に応じて、関数のラッピングをさらに行います。
Gem::Version
ここに行く簡単な方法です:
%w<0.1 0.2.1 0.44>.map {|v| Gem::Version.new v}.max.to_s
=> "0.44"
私は同じ問題を抱えていました、ジェムレスバージョンのコンパレータが欲しかったので、これを思いつきました:
def compare_versions(versionString1,versionString2)
v1 = versionString1.split('.').collect(&:to_i)
v2 = versionString2.split('.').collect(&:to_i)
#pad with zeroes so they're the same length
while v1.length < v2.length
v1.push(0)
end
while v2.length < v1.length
v2.push(0)
end
for pair in v1.zip(v2)
diff = pair[0] - pair[1]
return diff if diff != 0
end
return 0
end
Version
:すべてのIの必要性を行うクラスshorts.jeffkreeftmeijer.com/2014/...