Comparing versions in Ruby with develop-SNAPSHOT

时间:2019-03-19 15:02:11

标签: ruby

I am looking to compare package versions in ruby. Some of my package names are develop-SNAPSHOT.

When using something like

Gem::Version.new('develop-SNAPSHOT-1529333073') > Gem::Version.new('develop-SNAPSHOT') 

I get the response

ArgumentError: Malformed version number string develop-SNAPSHOT-1529333073

In python I would use packaging.version.parse() to compare similar strings to this and it would work fine with that, is there anything equivalent in Ruby?

1 个答案:

答案 0 :(得分:0)

根据doc

  

版本字符串通常应该是一系列由句点分隔的数字。每个部分(用句点分隔的数字)被认为是自己的编号,这些用于排序。例如,3.10比3.2高,因为十大于二。

因此,您的版本字符串('develop-SNAPSHOT-1529333073''develop-SNAPSHOT')的格式均错误。

您应该更改版本字符串的格式,例如到'1529333073'而没有前缀,一切都会好的。

如果要在版本字符串中包含前缀,则需要在使用Gem::Version.new()之前将其删除。
例如,下面的代码将按预期工作:

Gem::Version.new('develop-SNAPSHOT-1529333073'.sub(/develop-SNAPSHOT-?/, '')) > 
   Gem::Version.new('develop-SNAPSHOT'.sub(/develop-SNAPSHOT-?/, ''))

=> true