tim_xyz
tim_xyz

Reputation: 13601

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

I'm doing a tutorial on authentication and came across the following line for gemfile. What is the use of require here?

gem 'google-api-client', require: 'google/api_client'

Tutorial: http://willschenk.com/setting-up-devise-with-twitter-and-facebook-and-other-omniauth-schemes-without-email-addresses/

I understand require in Javascript, but in Rails I thought the gemfile is for installing gems, and once they are installed they can be used in the application, and thats all there is to it... so I'm not sure why I would use require.

I'm particularely interested because after adding this line and starting the server, I ran into an error.

Error:

/usr/local/rvm/gems/ruby-2.3.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:77:in `require': cannot load such file -- google/api_client (LoadError)

Temporary solution: I've commented out the require: part and the error is prevented. But maybe this is not ideal.

So understanding the use of require would help very much in troubleshooting this.

I read other articles on SO, but they discuss specifics like require => nil and require => false, which I think is a little different from my question.

  1. Bunder: What does :require => nil in Gemfile mean?
  2. Bundler: What does :require => false in a Gemfile mean?

Can anyone share some incite?

UPDATE

I later found this which explains it well: 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:

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.

Upvotes: 4

Views: 2943

Answers (1)

Lucas Caton
Lucas Caton

Reputation: 3255

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.

Upvotes: 3

Related Questions