Rails gemfile 中的 `require:` 是什么意思?

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

我正在做一个关于身份验证的教程,并遇到了 gemfile 的以下行。这里require有什么用?

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

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

我在Javascript中理解require,但在Rails中我认为gemfile是用来安装gems的,一旦安装它们就可以在应用程序中使用,就是这样所有的一切......所以我不确定为什么我会使用 require.

我特别感兴趣,因为在添加此行并启动服务器后,我 运行 出错了。

错误:

/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)

临时解决方案:我把require:部分注释掉了,就避免了这个错误。但也许这并不理想。

因此了解 require 的使用将对解决此问题有很大帮助。

我阅读了其他关于 SO 的文章,但他们讨论了 require => nilrequire => false 等细节,我认为这与我的问题有点不同。

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

任何人都可以分享一些煽动吗?

更新

后来我发现这个解释得很好: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.

当 gem 本身不需要任何库时,您需要在 Gemfile(您编写的方式)或项目中的某个文件中执行此操作。

例如:想象一个 gem 对于任何特定问题都有多个解决方案。但是,您不想加载所有这些解决方案(文件),您只需要一个。然后您需要使用 require: some_lib.

指定要加载的文件