I need some YAML parsing tips

I want to do some database population using YAML. Now, I have the
basics of how to read YAML, but am getting stuck on specifics. I’d use
fixtures, but the format isn’t what I’d like.

For instance, I want to do something like this:

Canada:
Vancouver
Edmonton
Calgary
etc…

And make a new record for each line under Canada, with Canada of
course being the key. How do I do this (syntax wise) with the YAML
processor?

And make a new record for each line under Canada, with Canada of
course being the key. How do I do this (syntax wise) with the YAML
processor?

The easiest way of thinking about YAML syntax IMHO is to regards it as
a hash of keys and values.

So your example would not work, because the cities are not key =>
value pairs but are trying to be an array.

But the easy way is just ask Ruby how to turn this into YAML.

First make the ruby implementation of what you want, maybe like this:

data = { ‘Canada’ => [ ‘Vancouver’, ‘Edmonton’, ‘Calgary’, ‘etc…’ ],
‘Australia’ => [ ‘sydney’, ‘Melbourne’, ‘Adelaide’, ‘etc…’ ] }

And then go into IRB and ask Ruby:

irb

irb(main):001:0> require ‘yaml’
=> true

irb(main):002:0> data = { ‘Canada’ => [ ‘Vancouver’, ‘Edmonton’,
‘Calgary’, ‘etc…’ ], ‘Australia’ => [ ‘sydney’, ‘Melbourne’,
‘Adelaide’, ‘etc…’ ] }

irb(main):003:0> puts data.to_yaml

Australia:

  • Sydney
  • Melbourne
  • Adelaide
  • etc…
    Canada:
  • Vancouver
  • Edmonton
  • Calgary
  • etc…

And there you have it. Your YAML syntax nicely formatted for you.

Mikel