On Mar 13, 2007, at 10:28 AM, ara.t.howard / noaa.gov wrote:

> On Wed, 14 Mar 2007, Mark Volkmann wrote:
>
>>
>> It seems to me this is a fundamental problem with YAML. Even if  
>> you were trying to put all the data in one file, I don't believe  
>> the YAML spec. addresses writing object references instead of  
>> objects. This is especially an issue when your objects have  
>> circular references.
>>
>
>> This caused me to use XML instead of YAML for a recent Java  
>> project because the Java XStream library handles serializing and  
>> deserializing Java objects that have circular references.
>>
>> If there is a YAML solution to this, I'd love to hear about it!
>
> harp:~ > ruby -r yaml -e'  h = {};  h[:h] = h;  y h  '
> &id001
> :h: *id001

My apologies! Apparently the problem is with the Java implementation  
of YAML that I was using and not with YAML itself. Here's a more full  
example that demonstrates YAML doing the right thing with multiple  
references to the same object and with circular references.

---

require 'yaml'

class Person
   attr_accessor :name, :spouse, :address

   def to_s
     "\n#{name} is married to #{spouse.name} and lives at\n#{address}"
   end
end

class Address
   attr_accessor :street, :city, :state, :zip

   def to_s
     "#{street}\n#{city}, #{state}  #{zip}"
   end
end

a = Address.new
a.street = "644 Glen Summit"
a.city = "St. Charles"
a.state = "MO"
a.zip = 63304

p1 = Person.new
p1.name = "Mark Volkmann"
p1.address = a

p2 = Person.new
p2.name = "Tami Volkmann"
p2.address = a

p1.spouse = p2
p2.spouse = p1

people = [p1, p2]
yaml_string = YAML::dump(people)
puts yaml_string

new_people = YAML::load(yaml_string)
puts new_people

---

The output is

---
- &id002 !ruby/object:Person
   address: &id001 !ruby/object:Address
     city: St. Charles
     state: MO
     street: 644 Glen Summit
     zip: 63304
   name: Mark Volkmann
   spouse: &id003 !ruby/object:Person
     address: *id001
     name: Tami Volkmann
     spouse: *id002
- *id003

Mark Volkmann is married to Tami Volkmann and lives at
644 Glen Summit
St. Charles, MO  63304

Tami Volkmann is married to Mark Volkmann and lives at
644 Glen Summit
St. Charles, MO  63304