# I need some YAML parsing tips

**URL:** <https://www.ruby-forum.com/t/i-need-some-yaml-parsing-tips/132101>\
**Category:** Rails\
**Created:** [March 17, 2008, 12:00am UTC](https://www.ruby-forum.com/t/i-need-some-yaml-parsing-tips/132101 "2008-03-17T00:00:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Backov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c4cdca/32.png) [@Backov](https://www.ruby-forum.com/u/Backov)\
**Post date:** [March 17, 2008, 12:00am UTC](https://www.ruby-forum.com/t/i-need-some-yaml-parsing-tips/132101/1 "2008-03-17T00:00:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Backov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c4cdca/32.png) [@Backov](https://www.ruby-forum.com/u/Backov)\
**Post date:** [March 17, 2008, 1:26am UTC](https://www.ruby-forum.com/t/i-need-some-yaml-parsing-tips/132101/2 "2008-03-17T01:26:31Z")

</div>

> 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
