The naked Ruby: Object serialization with YAML

YAML object serialization in Ruby comes really handy especially when you are working on a Rails console with a collection of ActiveRecord objects that you have modified and would like to save somewhere other than the underlying relational database — let’s say to create some test fixtures. To do this, simply open a file, serialize the collection, and close the file, like so:

File.open('fixtures.yml', 'w') { |f| f.puts People.find(:all).to_yaml }

The above code opens the file fixtures.yml for writing, writes the collection of all People found in the currently connected relational database in YAML format, and closes the file — in one stroke of code. That line also illustrates the use of block argument — the code between the curly braces — to simplify the management of the file. After the block argument is executed, File#open closes the file automatically.

You can do the same kind of thing in the plain interactive Ruby console, irb. All you need to do is to include the YAML module, like so:

require 'yaml'
networks = { :home => 'airport', :office => 'linksys' }
File.open('networks.yml', 'w') { |f| f.puts networks.to_yaml }

Once included, the YAML module allows all objects — except for procs, bindings, and blocks — to be serialized. And how do these objects look in the YAML format? Here is the output of the above example:

---
:office: linksys
:home: airport

Now, that is very readable, isn’t it? Like Ruby, while expressive and powerful, YAML remains very concise, simple, and readable. Hence the name, YAML Ain’t Markup Language.

Einstein loved simple things, too

Here is a more interesting example, the configuration file of a Pandora site:

--- !pandora.rubyveil.com/^config
http_server      :
    mount_point  : /pandora                 # /pandora
    port         : 80                       # 8181
smtp_server      :
    address      : relay.example.com        # localhost
    port         : 25                       # 25
registrar        :
    id           : pandora@example.com      #
    first_name   : Pandora                  #
    last_name    : RubyVeil                 #
    approval     : !ruby/symbol manual      # :manual, :automatic
    expiry       : 7                        # 7 (days)
library          :
    books        : Library/Books            # Library/Books
    designs      : Library/Designs          # Library/Designs
    applications : Library/Applications     # Library/Applications
history          : !ruby/symbol brief       # :brief, :verbose
scriptable       : [ Sandbox ]              # []
design           : Classic                  # Classic, ...

When reconstituted, the above text turns into a Ruby hashtable of hashtables:

{ 'http_server' => { 'mount_point' => 'pandora', 'port' => 80 }, ... }

which is done by loading the YAML file, like so:

config = YAML::load(File.open(filename))

If you find this intriguing, head over to the YAML and Ruby YAML sites to learn more about it.

Comment

Commenting is closed for this article.