From: Phillip Gawlowski Date: 2010-12-06T20:20:36+09:00 Subject: Re: Traverse YAML node tree with non-unique values On Mon, Dec 6, 2010 at 11:43 AM, Martin C. wrote: > I have a YAML document which I believe is valid (at least it would be > representable in XML): If Ruby can't read it, it's not valid. ;) SCNR > purchase_order: >  date: 10/12/2010 >  vendor: 12345 >  item: >    product: Tomatoes >    quantity: 5 >  item: >    product: Eggs >    quantity: 2 > > The problem is that using YAML.read on this produces a Hash in which > only one item occurs because (naturally) it has to be unique. > > How can one traverse the nodes in a YAML document like this? You could parse it like any ol' text file, and build your Hash by hand, with anything ending in a colon being a Hash key, whereas anything that appears multiple times become a nested Hash with the key that would normally be overwritten becoming the key to the nested hash, and the, in your example, product becomes the nested Hash's keys. Like so: "purchase_order" => { "date" => "10/12/2010", "vendor" => "12345", "item" => {"Eggs" => 2, "Tomatoes" => 5} } -- Phillip Gawlowski Though the folk I have met, (Ah, how soon!) they forget When I've moved on to some other place, There may be one or two, When I've played and passed through, Who'll remember my song or my face.