Forum: Ruby YAML - Time to give?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Thila T. (Guest)
on 2006-01-15 04:29
Guys-
I am trying to create an YAML file like the following:
3
--- Begin
ga:
  name: Georgia
  capital: savanah
  population: 1 mil
  cities:
      city:
         name: Atlanta
         zip: 30328
      city:
         name: Savanah
         zip: 30304
tx:
  name: Texas
  capital: Dallas
  population: 2 mil
  cities:
      city:
         name: Fortworth
         zip: 22222
      city:
         name: Dallas
         zip: 12345
#---End

I keep getting "parse" error while loading it to yaml. Even if I manage
to load with "-", I could never fetch the City and zip details. Can
anyone help me with this nested yaml file? thanks
unknown (Guest)
on 2006-01-15 06:16
(Received via mailing list)
Hi --

On Sun, 15 Jan 2006, thila thila wrote:

>         name: Atlanta
>         zip: 30328
>      city:
>         name: Savanah
>         zip: 30304

One problem is that you've just redefined the "city" key for this
hash.  So you'll end up with Savannah but no Atlanta.

> I keep getting "parse" error while loading it to yaml. Even if I manage
> to load with "-", I could never fetch the City and zip details. Can
> anyone help me with this nested yaml file? thanks

When I cut-and-pasted your text and did a YAML.load on it, it worked
fine (other than the logic error with the "city" key).  Do you have a
tab character in the file somewhere, instead of leading spaces?

Try this and see if it prints 30328:

str = <<EOM
ga:
   name: Georgia
   capital: Savannah
   population: 1 mil
   cities:
     Atlanta:
       zip: 30328
     Savannah:
       zip: 30304
EOM
require 'yaml'
info = YAML.load(str)
p info["ga"]["cities"]["Atlanta"]["zip"]


David

--
David A. Black
removed_email_address@domain.invalid

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
Thila T. (Guest)
on 2006-01-18 18:28
Thanks dave. I believe I had tab spaces, which caused load error -
correct me if I am wrong. You suggestion works. However, I wrote a
program that outputs the yaml file with hardcoded values. Then I edited
the output files to match my needs - works beautifully.

Thanks
This topic is locked and can not be replied to.