Object.to_yaml works but .to_yaml fails

I’ve implemented a custom to_yaml method for an object. When I call
to_yaml on the object everything works as expected.

rb(main):008:0> puts area.to_yaml

Area Description: Deltana, Tanana Flats
Polygons: []
Geocodes:
UGC: AKZ223
FIPS6: 002290
Circles: []

Ceiling:
Altitude:
=> nil

However when I put the object in an array I get the following error

irb(main):009:0> puts [ area ].to_yaml
TypeError: wrong argument type String (expected Data)
from /usr/lib/ruby/1.8/yaml.rb:391:in emit' from /usr/lib/ruby/1.8/yaml.rb:391:inquick_emit’
from /usr/lib/ruby/1.8/yaml/rubytypes.rb:107:in `to_yaml’
from (irb):9

Here is the implementation of Area#to_yaml

def to_yaml( options = {} )
{
AREA_DESC_YAML => self.area_desc,
ALTITUDE_YAML => self.altitude,
CEILING_YAML => self.ceiling,
CIRCLES_YAML => self.circles,
GEOCODES_YAML => self.geocodes.inject({}){|h,geocode|
h.merge( geocode.name => geocode.value )},
POLYGONS_YAML => self.polygons
}.to_yaml
end

AREA_DESC_YAML and the others are just strings like “Area Description”
etc. I’m not enough of a YAML expert to understand why serialization
fails when the area object is in an array.

Farrel

I seemed to have solved this. I had to pass the options hash through
to the to_yaml call on the hash created in the to_yaml method.

Farrel