这样做:
gem 'whenever', require: false
意味着需要安装gem,还是意味着不需要它?
这样做:
gem 'whenever', require: false
意味着需要安装gem,还是意味着不需要它?
当前回答
当你在你的Gemfile中指定一个Gem并运行bundle install时,bundle会安装指定的Gem并在你的应用程序中加载该Gem的代码,通过require ' Whenever ',这样bundle就会在你的Rails应用程序中加载所有Gem的代码,你可以从任何Gem中调用任何方法,就像你大多数时候做的那样。
but Gems like whenever, faker or capistrano are something which you do not need in your app code you need whenever code in your schedule.rb file to manage crons and capistrano code in deploy.rb file to customize deployment recipe so you need not to load code for these gems in your app code and 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.
其他回答
当你在你的Gemfile中指定一个Gem并运行bundle install时,bundle会安装指定的Gem并在你的应用程序中加载该Gem的代码,通过require ' Whenever ',这样bundle就会在你的Rails应用程序中加载所有Gem的代码,你可以从任何Gem中调用任何方法,就像你大多数时候做的那样。
but Gems like whenever, faker or capistrano are something which you do not need in your app code you need whenever code in your schedule.rb file to manage crons and capistrano code in deploy.rb file to customize deployment recipe so you need not to load code for these gems in your app code and 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.
类比解释
## Gemfile
gem "university_degree", require: false
gem "dealing_with_boss"
"dealing_with_boss" -总是需要的-加载到内存中并准备运行。 学位宝石-不需要. ...但如果需要,则需要在代码中手动要求它。
你使用:require => false当你想要安装宝石,但不是“必需的”。
在你给出的例子中: Gem '无论何时',:require => false 当有人运行bundle install时,无论何时都安装gem,就像安装gem时一样。用于通过运行rake任务来创建cron作业,但通常不从rails(或其他框架,如果不是rails)应用程序中使用。
因此,您可以使用:require => false用于需要从命令行运行但在代码中不需要的任何内容。
require: false告诉Bundler。Require不需要特定的宝石:必须通过Require 'gem'明确地需要宝石。
此选项不影响:
Bundle安装:无论如何gem都会被安装 要求搜索路径设置由捆绑。 Bundler添加东西到路径,当你做任何一个: Bundle.setup 它被require bundler/setup调用 它被bundle exec调用
例子
Gemfile
source 'https://rubygems.org'
gem 'haml'
gem 'faker', require: false
main.rb
# Fail because we haven't done Bundler.require yet.
# bundle exec does not automatically require anything for us,
# it only puts them in the require path.
begin Haml; rescue NameError; else raise; end
begin Faker; rescue NameError; else raise; end
# The Bundler object is automatically required on `bundle exec`.
Bundler.require
Haml
# Not required because of the require: false on the Gemfile.
# THIS is what `require: false` does.
begin Faker; rescue NameError; else raise; end
# Faker is in the path because Bundle.setup is done automatically
# when we use `bundle exec`. This is not affected by `require: false`.
require 'faker'
Faker
那么下面的代码不会引发异常:
bundle install --path=.bundle
bundle exec ruby main.rb
你可以在GitHub上玩。
Rails使用
正如在初始化教程中所解释的,默认的Rails模板在启动时运行:
配置/ boot.rb 配置/ application.rb
配置/ boot。rb包含:
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
它需要'bundler/setup'并设置require路径。
配置/应用程序。rb:
Bundler.require(:default, Rails.env)
这实际上需要宝石。
这意味着安装gem,但在启动Bundler时不调用require。因此您需要手动调用
require "whenever"
如果你想去图书馆的话。
如果你要这么做
gem "whenever", require: "whereever"
然后捆绑者将下载宝石命名,但会调用
require "whereever"
如果需要的库的名称与gem的名称不同,则通常使用这种方法。