管理 Rails 的 gem 版本/依赖项
简单介绍一下背景,我有很强的 C#/静态类型背景。因此我倾向于用 .dll 来思考。因此,如果我在一个项目中工作,我会引用我所需的 dll,仅此而已。
作为 Ruby 和 Rails 的新手,我发现我可能做错了什么。例如,我使用本地的 gems 在家里创建了一个 Rails 应用程序。使用另一台计算机(例如工作计算机),我尝试处理该项目,却发现我正在使用不同版本的 gem。执行捆绑安装后,我回到了工作项目。
我遇到的问题是我的宝石库变得“混乱”。我最终得到了同一个 gem 的多个版本。这是其他人的工作方式吗?当使用 gem(来自 require)时,它会默认为最新版本吗?我感觉好像我没有正确管理依赖项,尽管正如我所提到的,我是 Ruby 世界的新手。
我是否应该只包含我的依赖项,然后在每次有不同/缺少的依赖项时执行捆绑安装?如果我想升级到较新版本的 gem,会发生什么情况?是否会更新捆绑使用的 gemfile 并继续使用它?
Just a bit of background, I come from a strong C#/staticly typed background. Therefore I tend to think in terms of .dlls. So if I was working in a project, I'd reference my required dlls and that would be that.
Being new to Ruby and Rails I find I might be doing something wrong. For example, I create a Rails app at home using the gems I have locally. Using a different computer (say a work computer) I attempt to work on the project only to find I'm using different versions of the gems. After carrying out a bundle install I'm back to a working project.
The issue I have with this is that my gem library becomes 'messy'. I end up with several versions of the same gem. Is this the way others work? When using a gem (from a require) will it default to the latest version? I feel as if I'm not managing the dependencies correctly, though as I've mentioned I'm new to the world of Ruby.
Should I just include my dependencies, then perform a bundle install each time I have different/missing dependencies? What happens if I wish to upgrade to a newer version of a gem? Would it be a case of updating the gemfile that bundle uses and getting on with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,bundler 是使用 Ruby on Rails 处理依赖关系的方法。为什么 ?首先,因为它是附带的(至少对于 3.0 版本)。其次,因为它非常简单(与 Java 的 Maven 不同)。
功能的非详尽列表:
等。有关这方面的更多信息,请查看http://gembundler.com/rationale.html
关于您的问题:是的,如果没有指定,bundler 将采用可用的最新版本。
另外,我想添加一个缺点:您无法根据操作系统指定 gem 版本。例如,nokogiri 有 Linux 版本和 Win 版本。
Yes, bundler is the way to go to work with dependancies with Ruby on Rails. Why ? First, because it's shipped with it (at least for version 3.0). Second, because it's simple as hell (unlike maven with Java).
A non exhaustive list of feature :
and so on. For more on this, check this http://gembundler.com/rationale.html
Regarding your question : yes, bundler will take the latest version available is none is specified.
Also, I would add a disavantage : you cannot specify a gem version depending on the OS. For example, nokogiri has a linux version AND a win version.
当需要 gem 中的模块时,默认行为是假设您需要最新版本的 gem(如果您安装了多个 gem)。您可以通过在特定应用程序中指定您想要的版本来更改此设置,如下所示:
在您需要宝石中的任何东西之前。这确保了该应用程序将使用指定版本的 gem,即使安装了较新的版本也是如此。
当然,当您不再需要已安装的 gem 的过时版本时,或者如果您始终使用捆绑器,您可以清除它们:只需擦除所有内容并再次运行捆绑器即可安装所需的 gem。
另一个有用的工具是 Ruby 版本管理器 (RVM),除了处理不同版本的 ruby 之外,它还提供称为 gemset 的功能允许您将不同的应用程序或环境彼此隔离。也就是说,应用程序 A 可以拥有其单独的宝石集及其所有必需的宝石,而应用程序 B 可以拥有另一个宝石集仅包含其所需的宝石。这将大大减少依赖项中的混乱。
The default behaviour when requiring a module in a gem is to assume you want the latest version of the gem if you have more than one installed. You can change this by specifying which version you want in a specific application like this:
Before you require anything from the gem. This ensures that this application will use the specified version of the gem, even if a newer one is installed.
You can of course clean out obsolete versions of your installed gems whenever you don't need them again, or if you use bundler consistently: just wipe everything and run bundler again to get just your required gems installed.
Another useful tool is the Ruby Version Manager (RVM), in addition to handling different versions of ruby it provides a feature called gemsets which allows you to isolate different applications or environments from eachother. That is App A can have its separate gemset with all its required gems, and App B have another gemset with only its required gems. This will reduce the clutter in your dependencies quite a bit.