构建 Ruby on Rails 应用程序时指定 Gem 版本
我正在使用这个优秀的教程学习 Ruby on Rails,我注意到作者总是在 Gemfile 中指定 Gem 版本号(参见 此处,此处和 此处)。这是我应该养成的习惯吗?如果是这样,我如何知道要指定哪个版本?
I'm learning Ruby on Rails using this excellent tutorial, and I noticed that the author always specifies the Gem version number in the Gemfile (see here, here, and here). Is this something I should get in the habit of doing? If so, how do I know what version to specify?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议不要指定gem版本,直到您发现与特定gem不兼容。
Bundler 本身会锁定特定版本的宝石。您只需要记住在生产计算机上使用
bundle install --deployment
,并仅在开发计算机(或其他目录)上进行升级。对项目进行良好的测试覆盖非常重要。如果没有测试,您将不知道升级何时会破坏您的应用程序。我谦虚地建议以下路线:
rake test
(只是为了确保一切正常)bundle update
rake test< /code>
如果测试通过,则提交
Gemfile.lock
中的更改。如果测试未通过,并且您想要锁定给定的 gem,则在 Gemfile 中指定版本。您可以阅读有关 install --deployment 和 部署 在捆绑器文档中。
I suggest to not specify the gem version, until you have found an incompatibility with a specific gem.
Bundler will itself lock a specific versions of the gems. You just need to remember to use
bundle install --deployment
on the production machine(s), and make upgrades only on your development machine (or in another directory).It will be very important to have a good test coverage of your project. Without tests, you will not know when an upgrade breaks your application. I humbly suggest the following route:
rake test
(just to be sure that everything works)bundle update
rake test
If the test passes, commit the change in
Gemfile.lock
. If the test does not pass, and you want to lock a given gem, then specify the version in Gemfile.You may read about install --deployment and deploying in the bundler documentation.
“冻结”特定版本的宝石很重要,如果您正在准备生产某些东西,则必须这样做。 Gem、库和应用程序随着时间的推移而发展——为了发展,有时需要更改 Gem API,这通常会破坏与以前版本的向后兼容性。
有一些关于“冻结”Rails 版本的老文章(这就是我们过去所说的“指定 Gem 版本”),例如 DHH 的 Freeze 很酷,所以看在上帝的份上先冻结,以及 我应该冻结我的 Rails 应用程序吗?。
为了知道要指定哪个版本,我建议您指定最新版本。只需
gem install gemname
,然后gem list gemname
即可查看您刚刚安装的版本。"Freezing" gems at specific versions is important, and it is obligatory if you are preparing something for production. Gems, libraries and apps evolve with time -- and for evolving, sometime it is required to change the Gem API, and this offen brokes backward compatibility with previous versions.
There are a few old and good articles regarding "freezing" a Rails version (that's how we call "specifying a Gem version" in the old days), such as DHH's Freeze is cool, so freeze for goodness sake, and Should I freeze my Rails application?.
In order to know what version to specify, I recommend you specifying the latest version. Just
gem install gemname
and thengem list gemname
to see what version you just installed.