环境/*.rb中的配置符号是如何解析的 [英] How is the config symbol resolved in environments/*.rb
问题描述
典型的 config/environments/*.rb
文件是这样开始的:
This is how a typical config/environments/*.rb
file begins:
MyApp::Application.configure do
config.cache_classes = false
...
end
传递给 configure
的块取消引用了显然未绑定的符号 config
.这在技术上是如何工作的?块/Proc/lambda 中使用的符号应该在其声明的上下文中绑定,而不是留在调用站点的动态范围内解析.
The block passed to configure
dereferences the symbol config
which is apparently unbound. How does this technically work? The symbols used in a block/Proc/lambda should be bound in the context of its declaration, not left to be resolved in the dynamic scope at the call site.
一个相关的问题是,Application.configure
方法究竟在哪里声明?它不在 application.rb
中, engine.rb
或 railtie.rb
.也许如果我设法找到了这个方法,我就会找到我主要问题的答案.
A related question is, where exactly is the Application.configure
method declared? It's not in either application.rb
, engine.rb
, or railtie.rb
. Maybe if I managed to find this method, I would have found the answer to my main question.
同样相关,我研究了Rails 初始化过程 非常详细,我什至找不到提及config/environments/*.rb
文件.如果我知道 init 过程如何处理这些文件,那可能会对此有所了解.
Also related, I have studied the Rails initialization procedure in excruciating detail, and I can't find even a mention of a config/environments/*.rb
file. If I knew how these files were treated by the init procedure, that may shed some light on this.
推荐答案
It's a method config
in Rails::Application
in the railties gem in lib/rails/application.rb
返回一个 Application::Configuration
的实例,在 lib/rails/application/configuration.rb
中定义.
It's a method config
in Rails::Application
in the railties gem in lib/rails/application.rb
which returns an instance of Application::Configuration
, defined in lib/rails/application/configuration.rb
.
方法 configure
是从 autoload
ed 模块 Configurable
、lib/贡献给
,定义为Railtie
rails/railtie/configurable
The method configure
is contributed to Railtie
from the autoload
ed module Configurable
, lib/rails/railtie/configurable
, and is defined as
def configure(&block)
class_eval(&block)
end
这解释了为什么 configure
接受的块可以解析 config
符号.请注意,class_eval
是 rubyist 的另一个魔法,它使这项工作成功:它将传入块的 self
符号重新绑定到调用站点的类.
which explains why the block that configure
accepts can resolve the config
symbol. Note that class_eval
is another piece of rubyist magic that makes this work: it rebinds the passed-in block's self
symbol to the call site's class.
检查启动过程部分第一个文件中的注释,其中解释了所有这些优点的来源、方式和顺序,包括如何处理 /config/environments
目录.
Check the comments in the first file in the Booting Process section, which explains where, how and in what order all this goodness comes from, including how the /config/environments
directory is processed.
这篇关于环境/*.rb中的配置符号是如何解析的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!