Ruby:“require: false”是什么? Gemfile 中的意思是什么?
这是:
gem 'whenever', require: false
表示需要安装gem,还是表示不需要安装?
Does this:
gem 'whenever', require: false
mean that the gem needs to be installed, or does it mean it is not required?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这意味着安装gem,但不调用require当你启动 Bundler 时。 则需要手动调用
因此,如果您想使用该库,
。如果您这样做
,那么捆绑程序将下载指定的 gem,但会调用
如果需要的库名称与 gem 的名称不同,则通常会使用此方法。
This means install the gem, but do not call require when you start Bundler. So you will need to manually call
if you want to use the library.
If you were to do
then bundler would download the gem named whenever, but would call
This is often used if the name of library to require is different than the name of the gem.
您使用
:require =>; false
当您希望安装 gem 但不是“必需”时。所以在你给出的例子中:
gem '每当', :require =>;错误
当有人运行bundle install时,每当gem都会被安装,就像
gem install every
一样。每当用于通过运行 rake 任务来创建 cron 作业,但通常不在rails(或其他框架,如果不是rails)应用程序中使用。所以你可以使用
:require =>; false
对于您需要从命令行运行但在代码中不需要的任何内容。You use
:require => false
when you want the gem to be installed but not "required".So in the example you gave:
gem 'whenever', :require => false
when someone runs bundle install the whenever gem would be installed as with
gem install whenever
. Whenever is used to create cron jobs by running a rake task but isn't usually used from within the rails (or other framework if not rails) application.So you can use
:require => false
for anything that you need to run from the command line but don't need within your code.require: false
告诉Bundler.require
不要要求特定的 gem:必须通过require 'gem'
显式要求该 gem。此选项不会影响:
bundle install
:无论如何,gem 都会安装require
捆绑程序设置的搜索路径如何,都会安装。当您执行以下任一操作时,Bundler 会将内容添加到路径中:
Bundle.setup
require bundler/setup
调用bundle exec 调用
示例
Gemfile
main.rb
那么以下内容不会引发异常:
在 GitHub 上供您使用。
Rails 使用
正如初始化教程中所述,默认的 Rails 模板运行启动时:
config/boot.rb
config/application.rb
config/boot.rb
包含:require 'bundler /setup'
并设置所需路径。config/application.rb
的作用:这实际上需要 gem。
require: false
tellsBundler.require
not to require that specific gem: the gem must be required explicitly viarequire 'gem'
.This option does not affect:
bundle install
: the gem will get installed regardlessthe
require
search path setup by bundler.Bundler adds things to the path when you do either of:
Bundle.setup
require bundler/setup
bundle exec
Example
Gemfile
main.rb
Then the following won't raise exceptions:
On GitHub for you to play with it.
Rails usage
As explained in the initialization tutorial, the default Rails template runs on startup:
config/boot.rb
config/application.rb
config/boot.rb
contains:which does the
require 'bundler/setup'
and sets up the require path.config/application.rb
does:which actually requires the gems.
每当您在
Gemfile
中指定一个Gem并运行bundle install
时,bundler将去安装指定的gem并通过放置require'在您的应用程序中加载该Gem的代码每当这样时,捆绑器就会为 Rails 应用程序中的所有 Gem 加载代码,并且您可以从任何 Gem 调用任何方法,没有任何痛苦,就像大多数时候一样。
但是像
whenever、faker 或 capistrano
这样的 Gem 是您在应用程序代码中不需要的东西,只要您的schedule.rb
文件中的代码用于管理 cron 和 capistrano 代码,您就需要这样的东西。用于自定义部署配方的deploy.rb
文件,因此您无需在应用程序代码中加载这些 gem 的代码无论您想从这些 Gem 中调用任何方法,您都可以通过放置
require "whenever"
来手动请求这些 Gem。所以你把:require =>; false
在这些 Gem 的 Gemfile 中,这样捆绑器将安装该 Gem,但不会加载该 Gem 本身的代码,您可以随时执行此操作,只需在您的情况下简单地输入 require 'whenever' 即可。Whenever you specify a Gem in your
Gemfile
and runbundle install
, bundler will go and install specified gem and load code for that Gem in you app by puttingrequire 'whenever'
this way bundler will load code for all of your Gems in your Rails app, and you can call any method from any Gem without any pain, like you do most of the time.but Gems like
whenever, faker or capistrano
are something which you do not need in your app code you need whenever code in yourschedule.rb
file to manage crons and capistrano code indeploy.rb
file to customize deployment recipe so you need not to load code for these gems in your app codeand wherever you want to call any method from these Gems you can manually require thsese gems by yourself by putting
require "whenever"
. so you put:require => false
in your Gemfile for these Gems, this way bundler will install that Gem but not load code for that Gem itself, you can do it whenever you want by simply putting like require 'whenever' in your case.类比解释
“
dealing_with_boss
” - 加载到内存中并准备就绪。学位宝石
- 不是“需要”......您需要手动要求
它,才能使用它。Analogy to Explain
"
dealing_with_boss
" - loaded into memory and ready to go.degree gem
- not "needed"....you need to manuallyrequire
it, in order to use it.为了在 Gemfile 中需要 gem,您需要调用
Bundler.require
。您可以使用
require: false
阻止捆绑程序需要 gem,但它仍然会安装和维护 gem。检查这一点以获得更详细的解释。In order to require gems in your Gemfile, you will need to call
Bundler.require
.You can prevent bundler from requiring the gem with
require: false
, but it will still install and maintain the gem. Check this out for a more detailed explanation.