我有一个ruby字符串变量,其值为1.10.2.ab2-4.cl6
或1.11.2.ab2-4.dl5
或1.13.2.ab2-4.el6
我想获取此变量的前两个整数值,可以是1.10
或1.11
或1.13
,然后如果值为< 1.13
则打印hello
答案 0 :(得分:1)
假设这些是版本字符串,您可以使用Gem::Version
和Gem::Requirement
:
requirement = Gem::Requirement.new('< 1.13')
requirement.satisfied_by? Gem::Version.new('1.10.2.ab2-4.cl6') #=> true
requirement.satisfied_by? Gem::Version.new('1.11.2.ab2-4.dl5') #=> true
requirement.satisfied_by? Gem::Version.new('1.13.2.ab2-4.el6') #=> false