object.to_yaml works but [ object ].to_yaml fails

F

Farrel Lifson

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:in `quick_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
 
F

Farrel Lifson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top