~> 和有什么不一样?和 >= 在 Gemfile 中指定 rubygem 时?
我经常在 Gemfile 中看到以下符号(~>)。
gem "cucumber", "~>0.8.5"
gem "rspec", "~>1.3.0"
我知道符号 (>=) 只是大于或等于,但是 (~>) 符号是什么意思? 它们两者相同还是有显着差异?
I often see the following notation(~>) in Gemfile.
gem "cucumber", "~>0.8.5"
gem "rspec", "~>1.3.0"
I know the sign (>=) is just greater or equal to, but what does the (~>) notation mean?
Are they both same or has any significant difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个悲观版本约束。 RubyGems 将增加所提供版本中的最后一位数字并使用它,直到达到最大版本。因此
~>0.8.5
在语义上等同于:gem "cucumber", ">=0.8.5", "<0.9.0"
考虑一下,您可以将最后一位数字递增到某个任意值,但字符串中前面的数字不能大于您提供的值。因此,对于
~>0.8.5
,第三位(5)的任何值都可以接受,只要它大于或等于 5,但前导 0.8 必须是“0.8”。例如,如果您认为 0.9 版本将实现一些重大更改,但您知道整个 0.8.x 版本系列只是错误修复,您可能会这样做。
然而,简单地使用
“>=0.8.5”
将表明任何晚于(或等于)0.8.5的版本都是可接受的。没有上限。That's a pessimistic version constraint. RubyGems will increment the last digit in the version provided and use that until it reaches a maximum version. So
~>0.8.5
is semantically equivalent to:gem "cucumber", ">=0.8.5", "<0.9.0"
The easy way to think about it is that you're okay with the last digit incrementing to some arbitrary value, but the ones preceding it in the string cannot be greater than what you provided. Thus for
~>0.8.5
, any value is acceptable for the third digit (the 5) provided that it is greater than or equal to 5, but the leading 0.8 must be "0.8".You might do this, for example, if you think that the 0.9 version is going to implement some breaking changes, but you know the entire 0.8.x release series is just bugfixes.
However, simply using
">=0.8.5"
would indicate that any version later than (or equal to) 0.8.5 is acceptable. There is no upper bound.@millisami您甚至可以使用悲观约束来添加与gemspec的依赖关系,如下所示:
如果您对gem开发不太了解或刚刚开始接触它,那么这些是一些很好的参考:
@millisami You can even use to add dependencies with the gemspec using the pessimistic constraint like this:
If you don't know much about gem development or are just getting into it, these are some good references: