Messing around with YAML and Ruby

Software

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)

==>---
Tsundere:
  Clannad: Fujibayashi Kyou
  Bakemonogatari: Senjoughara Hitagi
moeness:
  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

Me!

Ruben Schade is a technical writer and infrastructure architect in Sydney, Australia who refers to himself in the third person. Hi!

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

If you found this post helpful or entertaining, you can shout me a coffee or send a comment. Thanks ☺️.