YAML(YAML Ain’t a Markup Language)

27 March 2012 by Gaurav Gaglani No comments

As the meaning of YAML its not markup language which could be used for communication between 2 components like while you are doing webservices and such. It is a mechanism to serailize data and use it accross your app. Ruby on Rails is a super example of how you could use it in the right way(database.yml). One tends to generally use for storing application level configurations (Like a properties file in Java but more readable and idomatic for me atleast). There are readymade parses available in languages like Ruby, Java, Perl, Python etc.

I would make sure to create a singleton object for your configuration as IO operations are expensive. Below are a few tips and tricks that I use regularly while manipulating configurations in using a yml file. It starts with basics and goes a little advanced (IMO, there is nothing so advanced though).

Basics:

username: 'someservice'
password: 'passme'
allowed: 5
....

the above assigns a basic key, value pair

grades:
  - 'A'
  - 'A-'
  - 'B+'
  - 'B'
  ...

array of grades. Sequences can also be defined as:

grades: ['A', 'A-', 'B+' ....]

Hashes:

companyaddress:
  street1: '100 W Hubbard'
  ...
  city: 'New York'
  state: ....

The above essentially means a element company address having a hash with k,v
companyaddress => {‘street1’ => ‘100 W Hubbard’, ‘city’ => ‘New York’}

Environment Specific:
This is so intuitive. If you need a set of values specific to the environment, you achieve it with almost no hastle:

defaults: &defaults
  correspondence_address:
    street1: '100 W Hubbard'
    ...
    city: 'New York'
    state: ....
development:
  <

What the above means is if you need a correspondence_address then its common in all environments where as; the billing_address could be fetched per environment(Obviously you will need some manipulations after you read the file to fetch the env specific values).

A little advanced:
A beautiful thing about YAML is aliases and references. Its like variabalizing your constants.
An alias is defined by &alias_name for example:

address: &address
  street1: '100 W Hubbard'
  ...
  city: 'New York'
  state: ....
  ....

A reference is accessed by *alias_name for example:

billing_address:
  <<:*address
shipping_address:
  <<:*address

The above means the shipping address and the billing address gets the values of address. Its like merging 2 hashes. An array can also be through references.

- &pass_grades
  - 'A'
  - 'A-'
  - 'B+'
  - 'B'
  ...
- &fail_grades
  - 'D'
  - 'F'
primary_school_grades:
  - *pass_grades #you will flatten the array to get the grade values
secondary_school_grades:
  # Again you will flatten the array to get the grade values
  - *pass_grades 
  - *fail_grades

Thus, if you have to add a third entry for grades for lets say a ‘high_school_grades’. You would just use the references for ‘pass_grades’ + ‘fail_grades’ like we do in the above example.

Ruby/Rails specific:
Below is just a mix of examples that I have found googling and used in my rails projects(its a real monster yeaahhh!!!).
For example, you want to add a email validation in your rails application and need a regex for it here is how you could do it.

email_regex: !ruby/regexp '/^([w.%+-]+)@([w-]+.)+([w]{2,})$/i'

You could also use erb expressions, obviously you need to take care of the dependencies.

development:
  cache_ttl: <%= 60.seconds %>
production:
  cache_ttl: <%= 60.minutes %>

References that might help:
http://en.wikipedia.org/wiki/YAML
http://yaml.org/YAML_for_ruby.html(for rubyists)

I will keep updating this blog as I learn more. But I hope the 3 minute read would have helped. Feel free to send opinions, examples and tricks through your comments.

Gaurav Gaglani

Follow me on Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Blog

Get access to proven marketing ideas, latest trends and best practices.

Next up home

Contact

Lets build cool stuff

Share your contact information & we will get in touch!

I want (Tell us more about your dream project)