bundle.setup 和 bundle.require 有什么区别 [英] what's the difference between bundle.setup and bundle.require
问题描述
从gemfile 手册页,我了解到有两种方法可以导入您在 Gemfile
中指定的 gem.bundle.setup
将安装程序将 gem 添加到 Ruby 的加载路径",而 bundle.require
将需要所有 gem.
From the gemfile man page, I learned there are two ways to import the gems you specified in the Gemfile
. The bundle.setup
will "setup adds gems to Ruby's load path" while bundle.require
will require all the gems.
这两种方法有什么区别?我应该在什么情况下使用其中之一?
What's the difference between these two methods? In which condition should I use one of them?
推荐答案
Bundler.setup
修改了 LOAD_PATH,所以你可以做像 require 'some_gem'
这样的事情将工作.它允许您手动"要求宝石.在 Bundler 之前,使用 Rubygems,您将获得与 require 'rubygems'
相同的效果.
Bundler.setup
modifies the LOAD_PATH, so you can do things like require 'some_gem'
and they will work. It allows you to require gems 'by hand'. Before Bundler, using Rubygems, you would achieve much of the same effect doing require 'rubygems'
.
Bundler.require(:default)
实际上需要 Gemfile 中的所有 gem(假设您没有使用组;否则,如果您提供参数,它需要指定组中的那些).它是一堆 require 'some_gem'
语句的简写.
Bundler.require(:default)
on the other hand actually requires all the gems in the Gemfile (assuming you're not using groups; otherwise it requires those in the specified groups if you provide arguments). It is a shorthand for a bunch of require 'some_gem'
statements.
请参阅 http://gembundler.com/rationale.html.请注意,他们说您必须在执行 Bundler.require
之前执行 require 'bundler/setup'
,但实际上这通常不是必需的.我几乎从不使用 Bundler.setup
(或 require 'bundler/setup
),因为我需要通过 Bundler.require
的所有 gem).
See http://gembundler.com/rationale.html. Note that they say you have to do require 'bundler/setup'
before doing Bundler.require
, but in practice this usually is not necessary. I almost never use Bundler.setup
(or require 'bundler/setup
), because I require all gems via Bundler.require
).
这篇关于bundle.setup 和 bundle.require 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!