How to compare versions in Ruby?

前端 未结 8 1936
耶瑟儿~
耶瑟儿~ 2020-12-12 19:47

How to write a piece of code to compare some versions strings and get the newest?

For example strings like: \'0.1\', \'0.2.1\', \'0.44\'.

相关标签:
8条回答
  • 2020-12-12 20:44

    If you need to check pessimistic version constraints, you can use Gem::Dependency like this:

    Gem::Dependency.new('', '~> 1.4.5').match?('', '1.4.6beta4')
    
    0 讨论(0)
  • 2020-12-12 20:44

    Gem::Version is the easy way to go here:

    %w<0.1 0.2.1 0.44>.map {|v| Gem::Version.new v}.max.to_s
    => "0.44"
    
    0 讨论(0)
提交回复
热议问题