YAML

YAML is a clear, human-readable markup language whose name stands for "YAML Ain't Markup Language." It's very popular in the Ruby community because the Ruby standard library includes a YAML parser (written by _why the lucky stiff).

YAML's file extension is .yml; this may be confusing to people who have never heard of YAML due to the similarity with the more popular .xml extension for XML files.

Unlike XML, YAML is used mostly as a human-editable serialization format. The YAML specification actually defines high-level constructs such as dates, associative arrays, lists, null, Symbols, integers, floating point numbers, etc. and includes a standard for coercing into these types.

Use in Ruby

Using YAML in Ruby is very easy. Below is a script which parses YAML from a String and from a file.

require "yaml"
content = "foo: bar"
p YAML.load(content) # => {"foo"=>"bar"}

data_from_file = YAML.load_file("config.yml")

If you have ActiveSupport installed, you can convert Ruby data structures into YAML

require 'rubygems'
require 'active_support'

complex_data_structure = [ {:name => "Bob", :age => 30}, {:name => "Bill", :age => 33} ]

puts complex_data_structure.to_yaml
This would output the following YAML:
---
- :name: Bob
  :age: 30
- :name: Bill
  :age: 33
The "---" header is a YAML idiom for denoting the text as YAML. Also, notice how the Symbol keys were preserved.

Resources

If you need help understanding how to read or write YAML, here are a few resources for you:


Browse Space

- Pages
- News
- Labels
- Attachments
- Bookmarks
- Mail
- Advanced
- Activity

Explore Confluence

- Popular Labels
- Notation Guide

Your Account

Log In

or Sign Up  

Other Features

Add Content