Gemfile中的“require: false”是什么意思?

时间:2022-11-13 00:15:17

Does this:

这样做:

gem 'whenever', require: false

mean that the gem needs to be installed, or does it mean it is not required?

表示需要安装gem,还是表示不需要它?

5 个解决方案

#1


408  

This means install the gem, but do not call require when you start Bundler. So you will need to manually call

这意味着安装gem,但是在启动Bundler时不要调用require。所以你需要手动调用

require "whenever"

if you want to use the library.

如果你想使用图书馆。

If you were to do

如果你要这么做的话

gem "whenever", require: "whereever"

then bundler would download the gem named whenever, but would call

然后bundler将下载命名的gem,但是会调用

require "whereever"

This is often used if the name of library to require is different than the name of the gem.

如果需要的库的名称与gem的名称不同,则通常使用此方法。

#2


67  

You use :require => false when you want the gem to be installed but not "required".

当您希望安装gem而不是“required”时,您可以使用:require => false。

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.

因此,在您给出的示例中:gem ' anywhere ',:require => false,当有人运行bundle安装时,每当gem安装时,就像gem安装时一样。无论何时,通过运行rake任务来创建cron作业,但是通常不在rails(或者其他框架,如果不是rails)应用程序中使用。

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,用于任何需要从命令行运行但在代码中不需要的内容。

#3


41  

require: false tells Bundler.require not to require that specific gem: the gem must be required explicitly via require 'gem'.

要求:假告诉打包机。不需要特定的宝石:必须通过“需要的宝石”明确地要求宝石。

This option does not affect:

此选项不影响:

  • bundle install: the gem will get installed regardless

    捆绑安装:不管怎样,gem都会被安装

  • the require search path setup by bundler.

    bundler查找路径设置。

    Bundler adds things to the path when you do either of:

    Bundler会在路径中加入以下两种内容:

    • Bundle.setup
    • Bundle.setup
    • which is called by require bundler/setup
    • 需要bundler/setup调用哪个
    • which is called by bundle exec
    • 哪个被bundle exec调用

Example

Gemfile

Gemfile

source 'https://rubygems.org'
gem 'haml'
gem 'faker', require: false

main.rb

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

Then the following won't raise exceptions:

那么下面的例子就不会出现例外:

bundle install --path=.bundle
bundle exec ruby main.rb

On GitHub for you to play with it.

你可以在GitHub上玩。

Rails usage

As explained in the initialization tutorial, the default Rails template runs on startup:

正如在初始化教程中解释的,默认的Rails模板在启动时运行:

  • config/boot.rb
  • 配置/ boot.rb
  • config/application.rb
  • 配置/ application.rb

config/boot.rb contains:

配置/ boot。rb包含:

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])

which does the require 'bundler/setup' and sets up the require path.

它执行所需的“bundler/setup”并设置所需路径。

config/application.rb does:

配置/应用程序。rb:

Bundler.require(:default, Rails.env)

which actually requires the gems.

这实际上需要宝石。

#4


8  

Whenever you specify a Gem in your Gemfile and run bundle install, bundler will go and install specified gem and load code for that Gem in you app by putting require '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.

每当你指定一个宝石Gemfile运行包安装,打包机去安装指定的宝石和宝石在你应用程序的加载代码将需要“每当”,这种方式打包机将代码加载所有的宝石在你的Rails应用程序,你可以调用任何方法从任何宝石没有任何痛苦,就像你大部分的时间。

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.

但是像“随时”、“伪造”或“capistrano”这样的珍宝是你在你的应用程序代码中不需要的东西。用于管理部署中的crons和capistrano代码的rb文件。rb文件来定制部署配方,所以您不需要在应用程序代码中加载这些gems的代码,而且无论您想要调用这些gems中的任何方法,您都可以通过在“随时”中输入require来手动要求thsese gems。所以你把:require => false在你的Gemfile中为这些宝石,这样bundler将安装这个Gem但不是装载代码,你可以在你想要的时候,只要你想要,只要你想要。

#5


0  

In order to require gems in your Gemfile, you will need to call Bundler.require.

为了在Gemfile中要求gems,需要调用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.

你可以用require: false来阻止bundler对宝石的要求,但是它仍然会安装和维护宝石。看看这个,有更详细的解释。

#1


408  

This means install the gem, but do not call require when you start Bundler. So you will need to manually call

这意味着安装gem,但是在启动Bundler时不要调用require。所以你需要手动调用

require "whenever"

if you want to use the library.

如果你想使用图书馆。

If you were to do

如果你要这么做的话

gem "whenever", require: "whereever"

then bundler would download the gem named whenever, but would call

然后bundler将下载命名的gem,但是会调用

require "whereever"

This is often used if the name of library to require is different than the name of the gem.

如果需要的库的名称与gem的名称不同,则通常使用此方法。

#2


67  

You use :require => false when you want the gem to be installed but not "required".

当您希望安装gem而不是“required”时,您可以使用:require => false。

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.

因此,在您给出的示例中:gem ' anywhere ',:require => false,当有人运行bundle安装时,每当gem安装时,就像gem安装时一样。无论何时,通过运行rake任务来创建cron作业,但是通常不在rails(或者其他框架,如果不是rails)应用程序中使用。

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,用于任何需要从命令行运行但在代码中不需要的内容。

#3


41  

require: false tells Bundler.require not to require that specific gem: the gem must be required explicitly via require 'gem'.

要求:假告诉打包机。不需要特定的宝石:必须通过“需要的宝石”明确地要求宝石。

This option does not affect:

此选项不影响:

  • bundle install: the gem will get installed regardless

    捆绑安装:不管怎样,gem都会被安装

  • the require search path setup by bundler.

    bundler查找路径设置。

    Bundler adds things to the path when you do either of:

    Bundler会在路径中加入以下两种内容:

    • Bundle.setup
    • Bundle.setup
    • which is called by require bundler/setup
    • 需要bundler/setup调用哪个
    • which is called by bundle exec
    • 哪个被bundle exec调用

Example

Gemfile

Gemfile

source 'https://rubygems.org'
gem 'haml'
gem 'faker', require: false

main.rb

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

Then the following won't raise exceptions:

那么下面的例子就不会出现例外:

bundle install --path=.bundle
bundle exec ruby main.rb

On GitHub for you to play with it.

你可以在GitHub上玩。

Rails usage

As explained in the initialization tutorial, the default Rails template runs on startup:

正如在初始化教程中解释的,默认的Rails模板在启动时运行:

  • config/boot.rb
  • 配置/ boot.rb
  • config/application.rb
  • 配置/ application.rb

config/boot.rb contains:

配置/ boot。rb包含:

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])

which does the require 'bundler/setup' and sets up the require path.

它执行所需的“bundler/setup”并设置所需路径。

config/application.rb does:

配置/应用程序。rb:

Bundler.require(:default, Rails.env)

which actually requires the gems.

这实际上需要宝石。

#4


8  

Whenever you specify a Gem in your Gemfile and run bundle install, bundler will go and install specified gem and load code for that Gem in you app by putting require '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.

每当你指定一个宝石Gemfile运行包安装,打包机去安装指定的宝石和宝石在你应用程序的加载代码将需要“每当”,这种方式打包机将代码加载所有的宝石在你的Rails应用程序,你可以调用任何方法从任何宝石没有任何痛苦,就像你大部分的时间。

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.

但是像“随时”、“伪造”或“capistrano”这样的珍宝是你在你的应用程序代码中不需要的东西。用于管理部署中的crons和capistrano代码的rb文件。rb文件来定制部署配方,所以您不需要在应用程序代码中加载这些gems的代码,而且无论您想要调用这些gems中的任何方法,您都可以通过在“随时”中输入require来手动要求thsese gems。所以你把:require => false在你的Gemfile中为这些宝石,这样bundler将安装这个Gem但不是装载代码,你可以在你想要的时候,只要你想要,只要你想要。

#5


0  

In order to require gems in your Gemfile, you will need to call Bundler.require.

为了在Gemfile中要求gems,需要调用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.

你可以用require: false来阻止bundler对宝石的要求,但是它仍然会安装和维护宝石。看看这个,有更详细的解释。