对Ruby字符串变量执行算术
我有一个ruby字符串变量,它的值可以是对Ruby字符串变量执行算术,ruby,ruby-on-rails-3,ruby-on-rails-4,rubygems,Ruby,Ruby On Rails 3,Ruby On Rails 4,Rubygems,我有一个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,然后打印hello,如果值是true 你是否满足了要求?Gem::Version.new('1.13.2.ab2-4.el6')#=>false 请定义
1.10.2.ab2-4.cl6
或1.11.2.ab2-4.dl5
或1.13.2.ab2-4.el6
我想取这个变量的前两个整数值,可能是1.10
或1.11
或1.13
,然后打印hello,如果值是<1.13
,假设这些是版本字符串,您可以使用和:
requirement=Gem::requirement.new(“<1.13”)
你是否满足了要求?Gem::Version.new('1.10.2.ab2-4.cl6')#=>true
你是否满足了要求?Gem::Version.new('1.11.2.ab2-4.dl5')#=>true
你是否满足了要求?Gem::Version.new('1.13.2.ab2-4.el6')#=>false
请定义<1.13
,这意味着如果值是1.10
或1.11
,但不是1.13
,那么1.2
是大于还是小于1.13
?“是更高版本”—这些版本号是什么?如果是这样,则1.2
将出现在1.13
之前,而不是之后。除非在1.20
中有尾随零。你能澄清一下吗?对不起。你说得对<代码>1.2将出现在1.13之前,而不是之后<代码>2.0将是更高版本。我将删除前面的评论
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