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

发布于 2024-10-13 21:53:09 字数 96 浏览 5 评论 0原文

这是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泡沫很甜 2024-10-20 21:53:09

意味着安装gem,但不调用require当你启动 Bundler 时。 则需要手动调用

require "whenever"

因此,如果您想使用该库,

。如果您这样做

gem "whenever", require: "whereever"

,那么捆绑程序将下载指定的 gem,但会调用

require "whereever"

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

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

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

require "whereever"

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

逆夏时光 2024-10-20 21:53:09

您使用 :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.

半岛未凉 2024-10-20 21:53:09

require: false 告诉 Bundler.require 不要要求特定的 gem:必须通过 require 'gem' 显式要求该 gem。

此选项不会影响:

  • bundle install:无论如何,gem 都会安装

  • require 捆绑程序设置的搜索路径如何,都会安装。

    当您执行以下任一操作时,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 模板运行启动时:

  • config/boot.rb
  • config/application.rb

config/boot.rb 包含:

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

require 'bundler /setup' 并设置所需路径。

config/application.rb 的作用:

Bundler.require(:default, Rails.env)

这实际上需要 gem。

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

  • the require search path setup by bundler.

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

    • Bundle.setup
    • which is called by require bundler/setup
    • which is called by bundle exec

Example

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

Then the following won't raise exceptions:

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

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:

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.

config/application.rb does:

Bundler.require(:default, Rails.env)

which actually requires the gems.

英雄似剑 2024-10-20 21:53:09

每当您在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 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.

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.

荒路情人 2024-10-20 21:53:09

类比解释

## Gemfile
gem "university_degree", require: false
gem "dealing_with_boss" 
  • dealing_with_boss” - 加载到内存中并准备就绪。

  • 学位宝石 - 不是“需要”......您需要手动要求它,才能使用它。

Analogy to Explain

## Gemfile
gem "university_degree", require: false
gem "dealing_with_boss" 
  • "dealing_with_boss" - loaded into memory and ready to go.

  • degree gem - not "needed"....you need to manually require it, in order to use it.

玩世 2024-10-20 21:53:09

为了在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文