YAML Rules the Config File and Data Serialization Universe

Though my recent explorations of Ruby and Ruby on Rails, I've become an acolyte in the church of YAML (YAML Ain't Markup Language). It's leagues simpler (and just better IMHO) than XML when used in the places where XML is most frequently deployed today - in config files and for data serialization.

For example, consider the following YAML config file that's used by Rails to define which databases it connects to:


development:
  adapter: mysql
  database: rails_development
  host: localhost
  username: root
  password: foobar
	
test:
  adapter: mysql
  database: rails_test
  host: localhost
  username: root
  password: foobar
	
production:
  adapter: mysql
  database: rails_production
  host: localhost
  username: root
  password: foobar

This is highly human-readable - almost intuitively obvious compared to XML. It's something you can very easily modify via the one true editor, a script, or anything else - but surprise: it's also a data structure (a hash of hashes) that can be accessed with almost zero effort. Observe finding the development database's hostname in Ruby:


require 'yaml'
config = YAML.load(File.open('config.yaml'))
	
puts config['development']['host']

Easy as pie.

Of course the above is just the tip of the iceberg. If you read through the spec from above, the Ruby YAML docs, or the YAML Ruby Cookbook, you'll find that YAML can represent very complex structures in a fashion that feels better than everything that's come before.

Highly recommended.

3 Responses to “YAML Rules the Config File and Data Serialization Universe”

  1. O'Rourke Says:

    I love these recommendations for development tools. Keep 'em coming. Thanks.

  2. q Says:

    Makes me think: YAML.ini

  3. Fabio Says:

    Just what I was looking for! Thank you.

Leave a Reply


Comment away... However, I reserve the right to remove SPAM without notice. I also reserve the right to edit stupid, offensive, or hateful comments. Alterations of this type will always be accompanied by a clear indication that censorship has occurred and a reason for its occurrence.