Messing around with YAML and Ruby


The Ruby Programming Language

Having studied Java almost exclusively at uni for a while, I decided while I had a holiday break I'd mess around with some Ruby goodness. Today was all about messing with YAML for a potential project thingy, to use technical terms.

To get started, the spiffy (though unfortunately named) dump method lets us see how a simple Ruby hash would look in YAML:

#!/opt/local/bin/ruby1.9 -w
require "yaml"
tsundere = {
  'Bakemonogatari' => 'Senjoughara Hitagi',
  'Clannad' => 'Fujibayashi Kyou'
puts YAML.dump(tsundere)

Clannad: Fujibayashi Kyou
Bakemonogatari: Senjoughara Hitagi

Seems simple enough. What if we created a hash of hashes and exported that? Would we get some nesting going on that makes semantic sense?

moeness = {
  'SOSDan' => 'Asahina Mikuru',
  'K-On!' => 'Akiyama Mio'
animegirls = {
  'Tsundere' => tsundere,
  'moeness' => moeness
puts YAML.dump(animegirls)

  Clannad: Fujibayashi Kyou
  Bakemonogatari: Senjoughara Hitagi
  K-On!: Akiyama Mio
  SOSDan: Asahina Mikuru

Shiny :). I suppose the next step is taking an existing YAML file and import it back. I imagine some nested blocks would be in order, no?

I've written some basic YAML flat file databases for myself before, but not with Ruby in mind. I've been told its excellent for object serialisation that's still human readable, but I'm more interested in being able to read/write preformatted data.

Where are you __why, we need your awesomeness back.

Author bio and support


Ruben Schade is a technical writer and IaaS engineer in Sydney, Australia who refers to himself in the third person in bios. Wait, not BIOS… my brain should be EFI by now.

The site is powered by Hugo, FreeBSD, and OpenZFS on OrionVM, everyone’s favourite cloud infrastructure provider.

You can leave me a comment by contacting me, and I might publish your thoughts. Please read the FAQs first though.

If you found this post helpful or entertaining, you can shout me a coffee or buy some silly merch. Thanks!