您何时需要Rails Gemfile中的需求? [英] When do you need a require in a rails Gemfile?

查看:47
本文介绍了您何时需要Rails Gemfile中的需求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的gemfile中,我有类似的东西:

In my gemfile I have things like:

gem 'net-sftp', '2.1.1', :require => 'net/sftp'
gem 'backup', '3.0.27'
gem 'watu_table_builder', :require => 'table_builder'
gem 'browser', '0.1.6'

在Gemfile中时您需要吗?我还发现了类似:require =>的内容。错误

When in a Gemfile do you need a require? I've also found things like :require => false. Help?

推荐答案

默认情况下,如果省略:require 选项, Bundler将尝试使用标准的名称到文件转换规则来要求gem:

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

这意味着以下gem语句

It means that the following gem statements

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

等效于

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

如果gem作者遵循标准约定,则此方法效果很好。但是在某些情况下,由于各种原因,这种情况不会发生。

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.

例如,有些宝石叫做 foo-bar ,其中主文件名是 /foo_bar.rb 甚至是 /foo.rb 。在这种情况下,您提供:require 选项来告诉Bundler您想要哪个文件。

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.

最后,要求:false 用于当您希望宝石成为捆绑包的一部分,但又不希望Bundler默认加载它时。

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.

在这种情况下,您传递了选项必需:false 。然后,在您的rake任务中,您将照常手动进行操作

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.

一个很好的例子是 任何时候 。该库必须是捆绑程序的一部分,因为在部署应用程序时必须捆绑该库,但该库应作为命令行脚本运行。因此,在启动Rails应用程序时,您不希望Bundler要求它。

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.

在某些情况下,您使用组而不是 require:false

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

另请参见 << c $ c> require 的官方捆绑商文档。

See also the official Bundler documentation for require.

这篇关于您何时需要Rails Gemfile中的需求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆