025. yaml 文件语法 - cwy007/tips-and-skills GitHub Wiki

1. 源码

# config/database.yml

default: &default
  adapter: sqlite3
  pool: 105
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# 可以进这个网址,将上面的代码转换成 json 格式 https://yaml-online-parser.appspot.com/
# Output
{
  "default": {
    "adapter": "sqlite3", 
    "pool": 105, 
    "timeout": 5000
  }, 
  "development": {
    "adapter": "sqlite3", 
    "pool": 105, 
    "timeout": 5000, 
    "database": "db/development.sqlite3"
  }
}

2. 解释

&default means you're labeling this set of attributes with some name for later use

<<: *default means you're including all attributes from group labeled as 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.

links:

https://yaml-online-parser.appspot.com/

https://stackoverflow.com/questions/6651275/what-do-the-mean-in-this-database-yml-file https://stackoverflow.com/questions/41063361/what-is-the-double-left-arrow-syntax-in-yaml-called-and-wheres-it-specced