对Ruby String变量

时间:2017-01-04 16:29:17

标签: ruby ruby-on-rails-3 ruby-on-rails-4 rubygems

我有一个ruby字符串变量,其值为1.10.2.ab2-4.cl61.11.2.ab2-4.dl51.13.2.ab2-4.el6

我想获取此变量的前两个整数值,可以是1.101.111.13,然后如果值为< 1.13则打印hello

1 个答案:

答案 0 :(得分:1)

假设这些是版本字符串,您可以使用Gem::VersionGem::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