Ruby: What Does 'Require: False' in Gemfile Mean

Ruby: What does 'require: false' in Gemfile mean?

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.

What does `require:` mean in a Rails gemfile?

When the gem itself doesn't require any of its lib, you need to do that either in your Gemfile (the way you wrote) or in some file in your project.

Eg.: imagine a gem which has more than one solution for any particular problem. However, you don't want to load all of those solutions (files), you only need one. Then you'd need to specify which file you want to load by using require: some_lib.

Why does rubocop recommend require: false in Gemfile?

It's just a guess, but the reason why require: false is suggested is because RubuCop is designed to be run as a CLI and not to be loaded as part of the environment when the application is started.

If you don't set require: false, Rails will load the gem. Even if the gem doesn't conflict with the app process, if you don't need it then loading the library it's an unnecessary overhead (both in terms of memory and allocation).

When do you need a require in a rails Gemfile?

If you omit the :require option, by default Bundler will attempt to require the gem by using the standard name-to-file conversion rule:

dashes are considered namespace separators and underscore classname separators

It means that the following gem statements

gem 'net-sftp'
gem 'backup'
gem 'foo_bar'

are equivalent to

gem 'net-sftp', require: 'net/sftp'
gem 'backup', require: 'backup'
gem 'foo_bar', require: 'foo_bar'

This works well if the gem author has followed the standard conventions. But in some cases, for a variety of reasons, this doesn't happen.

For instance, there are gems called foo-bar where the main filename is /foo_bar.rb or even /foo.rb. In this case you provide the :require option to tell Bundler which file you want to require.

Finally, require: false is used when you want a gem to be part of the bundle, but you don't want Bundler to load it by default.

This is useful, for instance, to lazy-load a gem in case it is used only in certain circumstances. Think about a rake task that includes an heavy gem. You don't want your application to load it on boot, but it needs to be part of the bundle or it will not be found.

In this case you pass the option require: false. Then, in your rake task you will require it manually as usual

require 'library'

The library will be loaded only when the task is invoked, not in the normal application execution.

A good example is whenever. The library must be part of the bundler because it must be bundled when you deploy the app, but it is intended to be run as a command line script. For this reason, you don't want Bundler to require it when the Rails application is started.

There are cases where you use groups instead of require: false.

See also the official Bundler documentation for require.

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

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

Do we need require and Gemfile at the same time?

The purpose of the Gemfile can be helpful so that you can insure your code will work by using bundler which allows you to run bundle install which will install the gems to work with the current version of ruby you will be using for your code. It will also add a Gemfile.lock file which is a good idea to commit in your version control to insure you have a working stack where the gems and the ruby version are all compatible.

If you only require the files in your script, there is no guaranteed that the gems are actually installed in the scope of that script's runner. So by having a Gemfile and Gemfile.lock and using bundler, you can have portability for your codebase.

Update

As per comment by @engineersmnky , you can specify gem version however with this syntax and it should work so long as those gems are installed. You would first need to make sure to install the version in your terminal:

gem install google_drive -v 2.1.11

Then you can do this in your ruby file

require 'rubygems' 
gem 'google_drive', '2.1.11';
require 'google_drive'
require 'typhoeus'
require 'json'
require 'pg'
require 'mandrill-api'

Gemfile with exact versions vs Gemfile.lock

No, an exactly specified Gemfile and using a Gemfile.lock is not the same.

Your Gemfile might include all gems you are using with a specific version. But the Gemfile.lock will also include all gems that are dependencies of the gem you use. That means that a typical Gemfile.lock will include way more gems when a Gemfile.

Furthermore: You might have gems or gem versions to your Gemfile that are incompatible with each other. A Gemfile.lock is generated by bundler and represents a set of gem versions that are compatible with each other. If bundler is not able to fulfill all required dependencies then it will not generate a Gemfile.lock.

That said: Pin only versions in your Gemfile that you need to pin because of version requirements of your app. Let bundler find a valid combination and check that Gemfile.lock into version control system.



Related Topics



Leave a reply



Submit