YAML by Example: Aliases
1.2
Reference anchors. Reuse configuration blocks.
Code
defaults: &defaults
adapter: postgres
host: localhost
development:
database: dev_db
<<: *defaults # Merge key
test:
database: test_db
<<: *defaults
# Simple alias example
favorite_fruit: &fruit Apple
breakfast: *fruit # Value is "Apple"
lunch: *fruit # Value is "Apple"Explanation
Aliases insert previously anchored data using the * symbol followed by the anchor name. The merge key << is special syntax that merges all keys from the anchored mapping into the current one, allowing you to inherit settings and override specific values.
Simple aliases copy scalar values or entire structures. The merge key only works with mappings and provides an inheritance-like pattern common in configuration files for development, staging, and production environments.
Code Breakdown
7
<<: *defaults merges the keys from "defaults" into "development".15
*fruit injects the value "Apple" here.
