What do the &,<<, * mean in this database.yml file?

后端 未结 5 2043
太阳男子
太阳男子 2020-12-22 15:40

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

5条回答
  •  暖寄归人
    2020-12-22 16:21

    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.

    Allow me to quote the YAML spec here:

    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".

    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)

提交回复
热议问题