~> 和有什么不一样?和 >= 在 Gemfile 中指定 ruby​​gem 时?

发布于 2024-10-05 06:27:40 字数 181 浏览 3 评论 0原文

我经常在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎歌 2024-10-12 06:27:40

这是一个悲观版本约束。 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.

遥远的绿洲 2024-10-12 06:27:40

@millisami您甚至可以使用悲观约束来添加与gemspec的依赖关系,如下所示:

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

如果您对gem开发不太了解或刚刚开始接触它,那么这些是一些很好的参考:

  1. 教您如何制作自己的 RubyGem、与之相关的标准实践以及如何上传它以便其他人可以安装的教程.
  2. 如何使用 Bundler 从头开始​​创建 Gem

@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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文