Ruby yaml custom domain type does not keep class

0 down vote favorite

I’m trying to dump duration objects (from the ruby-duration gem) to yaml
with a custom type, so they are represented in the form hh:mm:ss. I’ve
tried to modify the answer from this question:

but when parsing the yaml
with YAML.load, a Fixnum is returned instead of a Duration.
Interestingly, the Fixnum is the total number of seconds in the
duration, so the parsing seems to work, but convert to Fixnum after
that.

An example output in irb:

irb> Duration.new(27500).to_yaml
=> “— !example.com,2012-06-28/duration 7:38:20\n…\n”
irb> YAML.load(Duration.new(27500).to_yaml)
=> 27500

should be <Duration:0xxxxxxx @seconds=20, @total=27500, @weeks=0,

@days=0, @hours=7, @minutes=38>

See attachment for code