How to compare versions in Ruby?

前端 未结 8 2017
耶瑟儿~
耶瑟儿~ 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:38

    You can use the Versionomy gem (available at 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
    

提交回复
热议问题