Ruby-on-rails 这个 database.yml 文件中的 &、<<、* 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6651275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What do the &,<<, * mean in this database.yml file?
提问by OpenCoderX
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?
到目前为止,我只使用了 database.yml 并显式调用了每个参数,在下面的文件中它使用了一些我不理解的字符。每行和符号(&,*,<<) 是什么意思,我如何读取这个文件?
development: &default
adapter: postgresql
database: dev_development
test: &test
<<: *default
database: test_test
cucumber:
<<: *test
production:
<<: *default
database: test_production
回答by Pascal
The &marks an alias for the node (in your example &defaultaliases the development node as "default") and the *references the aliased node with the name "default". The <<:inserts the content of that node.
该&标志为节点的别名(在你的榜样&default别名开发节点为“默认”)和*引用名为“默认”的别名节点。在<<:插入该节点的内容。
Allow me to quote the YAML spec here:
请允许我在这里引用 YAML 规范:
Repeated nodes (objects) are first identified by an anchor (marked with the ampersand - “&”), and are then aliased (referenced with an asterisk - “*”) thereafter.
重复的节点(对象)首先由一个锚点(用与号 - “&”标记)标识,然后被命名为别名(用星号 - “*”引用)。
So parts of your example
所以你的例子的一部分
development: &default
adapter: postgresql
database: dev_development
test: &test
<<: *default
database: test_test
actually expand to
实际上扩展到
development: &default
adapter: postgresql
database: dev_development
test: &test
adapter: postgresql # from the "default" alias
database: test_test # overridden by the duplicate key
and at the same time make the "test" node as well available under the alias "test".
同时使“test”节点也可以在别名“test”下使用。
Have a look at the YAML specification - 2.2 Structuresfor further details (or if you need even moar docs++: 3.2.2.2. Anchors and Aliases)
查看YAML 规范 - 2.2 Structures以获取更多详细信息(或者如果您甚至需要 moar docs++: 3.2.2.2. Anchors andAliases )
回答by keymone
&defaultmeans you're labeling this set of attributes with some name for later use
&default意味着您正在用某个名称标记这组属性以供以后使用
<<: *defaultmeans you're including all attributes from group labeled as default
<<: *default意味着您要包括标记为默认的组中的所有属性
回答by Sam Ruby
回答by Mohammad Shahid Siddiqui
In simple words, this notion resembles with the base and derived class.
简单来说,这个概念类似于基类和派生类。
In base class template, you mention all the common details with '&', which means it can be used to expand the other yaml section that needs these fields. Now when you create another section that is superset of config values of this 'base class' type structure, you use the '*' along with the base class anchor (i.e. the one started with '&'). You use '<<:' as yaml notion for actually placing the 'base class' section, that you can override later.
在基类模板中,您使用“&”提及所有常见细节,这意味着它可用于扩展需要这些字段的其他 yaml 部分。现在,当您创建另一个部分是此“基类”类型结构的配置值的超集时,您可以使用“*”和基类锚点(即以“&”开头的锚点)。您使用“<<:”作为 yaml 概念来实际放置“基类”部分,您可以稍后覆盖。
vsm:
stub_nsx_mgr: &MGR_CTRL_STUB
username: ADMIN
password: $DEFAULT_PASSWORD
deployment: ovf
build: $PR_BUILD
vmnics:
- network: $MANAGEMENT_NETWORK_0
vc: vc_0
ovf_options:
- --diskMode=$DISKMODE
- --deploymentOption=$DEPLOYMENT_OPTION
$MGR_0:
<<: *MGR_CTRL_STUB
ovf_path_regex: 'appliance.*\.ovf'
ovf_options:
- --diskMode=$DISKMODE
- --deploymentOption=$DEPLOYMENT_OPTION
$CTRL_0:
<<: *MGR_CTRL_STUB
ovf_options:
- --diskMode=$DISKMODE
- --allowExtraConfig
$CTRL_1:
*MGR_CTRL_STUB
But, if you do not want to override the extended fields, you can skip '<<:'
但是,如果您不想覆盖扩展字段,则可以跳过 '<<:'
回答by thenengah
They are a way to reference environments without having to repeat the same settings over and over (DRY it up).
它们是一种引用环境的方法,而不必一遍又一遍地重复相同的设置(干掉它)。
test: &test
<<: *default
&testcreates a reference to those specific settings.
&test创建对这些特定设置的引用。
<<: *defaultsays use the default settings for the test
<<: *default说使用测试的默认设置
cucumber:
<<: *test
So now we know that for cucumberwe want to use the settings from test.
所以现在我们知道cucumber我们想要使用test.

