Bundler: What does :require => nil in Gemfile mean

2019-05-03 06:58发布

问题:

What does the nil mean in this gemfile entry?

gem "hub", ">= 1.10.2", :require => nil

I found this question and answer for false;

Bundler: What does :require => false in a Gemfile mean?

In this context, does nil behave the same as false?

回答1:

Yes, nil and false behave the same here: it makes Bundler not require the specified gem.



回答2:

Require nil or false means that bundler will not load (require) the specific gems. However, they will be in the $: load paths, so you can require them explicitly any time you want to use them. It is a good practice to use this, for gems that are only needed in special cases (e.g. external scripts, rake tasks etc.).



回答3:

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.

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 or :require => nil in your Gemfile(both means the same) 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.