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

OpenCoderX picture OpenCoderX · Jul 11, 2011 · Viewed 38.9k times · Source

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

Answer

Pascal picture Pascal · Jul 11, 2011

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)