The greatnesses and gotchas of YAML

http://yaml.org/ homepage
---
some_key:
some_other_key: nil
{'some_key' => {'some_other_key' => 'nil'}}
{'some_key': {'some_other_key': 'nil'}}
---
some_key:
some_other_key:
{'some_key' => {'some_other_key' => nil}}
{'some_key': {'some_other_key': None}}
---
some_key:
some_other_key: null

And this was only the start…

Inheritance

default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3

Write JSON in your YAML

---
key: {"some": "json"}
another: [1, 2, 3]
{"hello"=>{"some"=>"json"}, "another"=>[1, 2, 3]}

YAML keys as Ruby symbols

---
:my_symbol_key: :or_value
{:my_symbol_key=>:or_value}
{':my_symbol_key': ':or_value'}

Multiline strings? YAML’s got your back!

literal: |
This block of text will be the value of the 'literal' key,
with line breaks being preserved.

It continues until de-dented, leading indentation is
stripped.

Any lines that are 'more-indented' keep the rest
of their indentation -
these lines will be indented by 4 spaces.
folded: >
This block of text will be the value of 'folded', but this
time, all newlines will be replaced with a single space.

Blank lines, like above, are converted to a newline character.

'More-indented' lines keep their newlines, too -
this text will appear over two lines.

Quoted strings, begone!

some_key: with a string value
{"some_key"=>"with a string value"}
{"some key"=>"with a string value"}
# All the following equal true
YAML.load("key: Yes")
YAML.load("key: yes")
YAML.load("key: YES")
YAML.load("key: on")
YAML.load("key: On")
YAML.load("key: ON")
# => {"key"=>true}
# All the following equal false
YAML.load("key: no")
YAML.load("key: No")
YAML.load("key: NO")
YAML.load("key: off")
YAML.load("key: Off")
YAML.load("key: OFF")
# => {"key"=>false}

Casting values

---
key: !!python/tuple [1, 2]
{key: (1, 2)}
{"key" => [1, 2]}
---
key: !!ruby/array [1, 2]
{"key" => [1, 2]}
...snipped...
yaml.constructor.ConstructorError: could not determine a constructor for the tag 'tag:yaml.org,2002:ruby/array'
in "<unicode string>", line 1, column 6:
key: !!ruby/array [1, 2]

Integer notation

Sexagesimal numbers?

YAML.load("key: 12:30:00")
# => {"key"=>45000}
YAML.load("key: 01:30:00")
# => {"key"=>5400}
yaml.safe_load("key: 01:30:00")
# => {'key': '01:30:00'}

Octal numbers

# parsed as octal
YAML.load("key: 0123")
# => {"key": 83}
# parsed 'normally'
YAML.load("key: 01238")
# => {"key": "01238"}

Complex keys

---
? !!python/tuple [1, 2]
: hello
{(1, 2): 'hello'}
{[1, 2] => "hello"}

Comments

---
some: yaml
# oh noes! A comment
no: problem
{"key"=>[1, 2], "key2"=>"no problem"}

Summary

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store