3

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.

Community
  • 1
  • 1
tim_xyz
  • 11,573
  • 17
  • 52
  • 97
  • Try adding the 'require..' to the relevant model where you utilize the gem and don't include that part in the gem file. – Carson Cole Apr 20 '16 at 05:05
  • usually that error means that there is no `google/api_client` file in `google-api-client` gem's library folder, check it manually by issuing in bash: `file $(bundle show google-api-client)/lib/google/api_client` – Малъ Скрылевъ Apr 20 '16 at 07:03
  • I can actually cd right into `api_client` directory - `/usr/local/rvm/gems/ruby-2.3.0/gems/google-api-client-0.9.5/lib/google/api_client` . Could it be looking in the wrong place for it? – tim_xyz Apr 20 '16 at 07:39

1 Answers1

2

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.

Lucas Caton
  • 3,027
  • 1
  • 24
  • 34
  • This is strictly done for performance reasons and excluding `require` will have no negative implications on app functionality? – tim_xyz Apr 20 '16 at 11:17
  • @pandaman not necessarily. Depending on how the gem is structured, you do need to add the `require`. – Lucas Caton Apr 22 '16 at 01:09