Programming With Ruby Episode 14, YAML

by Tyler on July 24th, 2009
Programming With Ruby Episode 13, Basic I/OProgramming With Ruby Episode 15, Error Handling

Covered in this Episode:

  • What is YAML
  • Why should you use YAML?
  • Storing Data

Transcript:

Hello Everybody and welcome to Programming With Ruby Episode 14,
YAML. I’m Tyler and this video is brought to you by manwithcode.com.

I will start off this episode telling you what YAML is and why you
should be using it. I will then be showing you how you can store
objects and various assorted other kinds of data using YAML.

Without further delay, lets get started!

What is YAML?

YAML stands for YAML Ain’t Markup Language. In the most basic sense it
is a way of storing data for later use.

Why should you use YAML?

Lets say you are writing a game, you could save your players progress
to a YAML file. Or if your writing an application with many preference
settings, you might save the user’s preferences to a YAML file.

Using YAML instead of creating your own text-based format creates more
portability, because other applications can immediately use your YAML
file instead of having to write their own custom file loader.

YAML also makes editing files by hand very easy to do if you have to.

Enough of all the theoretical talk, lets write some code!

Storing Data

Lets say we are writing a text editor and there are a few preferences the
user can change. We want the user to be able to set their preferences and
have them saved, so the next time they start up our text editor the
preferences are set the same as they were at last use.

We decide to use YAML to store our users preferences. Lets say for
example that the preferences are stored in a hash:

@preferences = {"word-wrapping" => true, "font-size" => 20, "font" => "Arial"}

To save that using YAML we have to use:

require 'yaml'

Open a file for writing (YAML files end in .yml or .yaml):

output = File.new('prefs.yml', 'w')

Use the YAML.dump method to get the text that will be outputted:

output.puts YAML.dump(@preferences)
output.close

Full source:

require 'yaml'
@preferences = {"word-wrapping" => true, "font-size" => 20, "font" => "Arial"}

output = File.new('prefs.yml', 'w')
output.puts YAML.dump(@preferences)
output.close

And that is how easy it is to store data!

We read back a file in a similar way, using the YAML.load method:

require 'yaml'

output = File.new('prefs.yml', 'r')
@preferences = YAML.load(output.read)
output.close

Don’t think you’re limited to simple data like hashes and arrays, you
can store objects too!

It has been my experience that it is usually best to store data in
hashes, because they automatically have a label, unlike an array. You
can do whatever you want to, but that is my recommendation.

Now comes the sad time when we have to close the episode.

Please don’t forget to donate, a few dollars can go a long way.

If you have any questions, comments or suggestions, please do not
hesitate to leave a comment below or email me at tyler@manwithcode.com

Thank for watching!

Bye!

Programming With Ruby Episode 13, Basic I/OProgramming With Ruby Episode 15, Error Handling

Be Sociable, Share!

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS