Bundler:在Gemfile中要求=> false ?

时间:2022-11-09 00:15:49

The line is this one:

这条线是这样的:

gem 'whenever', :require => false

Does it means that the gem needs to be installed or is not required?

这是否意味着gem需要安装或者不需要?

5 个解决方案

#1


406  

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

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

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会下载名为“任何时候”的宝石,但会打电话。

require "whereever"

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

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

#2


65  

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.

因此,在您给出的示例中:“when”,: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


40  

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

    bundle安装:无论如何,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.

但是像任何时候,faker或capistrano这样的宝石在你的应用程序代码中是不需要的,只要你的时间表中有代码。rb文件管理crons和capistrano代码的部署。rb文件可以定制部署配方,因此您无需在应用程序代码中加载这些gem的代码,无论您想从这些gem中调用任何方法,您都可以手动要求thsese gems,只要您需要“随时”即可。所以你把:require => false在你的Gemfile中为这些宝石,这样bundler将安装这个Gem但不是装载代码,你可以在你想要的时候,只要你想要,只要你想要。

#5


2  

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

为了在您的Gemfile中需要gem,您需要调用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.

您可以防止bundler需要的gem需要:false,但是它仍然会安装和维护gem。请检查这个以得到更详细的解释。

#1


406  

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

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

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会下载名为“任何时候”的宝石,但会打电话。

require "whereever"

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

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

#2


65  

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.

因此,在您给出的示例中:“when”,: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


40  

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

    bundle安装:无论如何,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.

但是像任何时候,faker或capistrano这样的宝石在你的应用程序代码中是不需要的,只要你的时间表中有代码。rb文件管理crons和capistrano代码的部署。rb文件可以定制部署配方,因此您无需在应用程序代码中加载这些gem的代码,无论您想从这些gem中调用任何方法,您都可以手动要求thsese gems,只要您需要“随时”即可。所以你把:require => false在你的Gemfile中为这些宝石,这样bundler将安装这个Gem但不是装载代码,你可以在你想要的时候,只要你想要,只要你想要。

#5


2  

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

为了在您的Gemfile中需要gem,您需要调用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.

您可以防止bundler需要的gem需要:false,但是它仍然会安装和维护gem。请检查这个以得到更详细的解释。