开发者

What is the difference between ~> and >= when specifying rubygem in Gemfile?

开发者 https://www.devze.com 2023-01-27 09:33 出处:网络
I often see the 开发者_Go百科following notation(~>) in Gemfile. gem \"cucumber\", \"~>0.8.5\" gem \"rspec\", \"~>1.3.0\"

I often see the 开发者_Go百科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?


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 You can even use to add dependencies with the gemspec using the pessimistic constraint like this:

gem.add_runtime_dependency "thor", "~> 0.18.1"

If you don't know much about gem development or are just getting into it, these are some good references:

  1. Tutorial that teaches you how to make your own RubyGem, the standard practices associated with it, and how to upload it so that others can install it.
  2. How to create a Gem from scratch with Bundler
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号