问题描述
到目前为止,我只使用了明确调用每个参数的 database.yml,在下面的文件中它使用了一些我不理解的字符.每行和符号(&,*,<<) 是什么意思,我如何读取这个文件?
Up until now I have only used database.yml with each parameter called out explicitly, in the file below it uses some characters I do not understand. What does each line and symbol(&,*,<<) mean, how do i read this file?
development: &default
adapter: postgresql
database: dev_development
test: &test
<<: *default
database: test_test
cucumber:
<<: *test
production:
<<: *default
database: test_production
推荐答案
&
标记节点的别名(在您的示例中,&default
为开发设置别名节点作为默认")并且 *
引用名称为默认"的别名节点.<<:
插入该节点的内容.
The &
marks an alias for the node (in your example &default
aliases the development node as "default") and the *
references the aliased node with the name "default". The <<:
inserts the content of that node.
请允许我在这里引用 YAML 规范:
Allow me to quote the YAML spec here:
重复的节点(对象)首先由一个锚点标识(用 & 符号标记 - &"),然后再使用别名(用星号引用 - *").
所以你的例子的一部分
development: &default
adapter: postgresql
database: dev_development
test: &test
<<: *default
database: test_test
实际上展开为
development: &default
adapter: postgresql
database: dev_development
test: &test
adapter: postgresql # from the "default" alias
database: test_test # overridden by the duplicate key
同时使test"节点在别名test"下也可用.
and at the same time make the "test" node as well available under the alias "test".
查看 YAML 规范 - 2.2 结构 了解更多信息详细信息(或者如果您甚至需要 moar docs++:3.2.2.2. 锚和别名)
Have a look at the YAML specification - 2.2 Structures for further details (or if you need even moar docs++: 3.2.2.2. Anchors and Aliases)
这篇关于&,<<, * 在这个 database.yml 文件中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!