# Yaml: custom saving and loading of specific classes

**URL:** <https://www.ruby-forum.com/t/yaml-custom-saving-and-loading-of-specific-classes/52241>\
**Category:** Rails\
**Created:** [January 12, 2006, 5:07am UTC](https://www.ruby-forum.com/t/yaml-custom-saving-and-loading-of-specific-classes/52241 "2006-01-12T05:07:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob84123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bob84123](https://www.ruby-forum.com/u/bob84123)\
**Post date:** [January 12, 2006, 5:07am UTC](https://www.ruby-forum.com/t/yaml-custom-saving-and-loading-of-specific-classes/52241/1 "2006-01-12T05:07:21Z")

</div>

I’m trying to use ruby’s yaml library to serialise and deserialise some  
legacy classes. Simply adding a to\_yaml\_properties() method isn’t  
sufficient as special methods need to be called to get and set the  
properties (they’re not stored simply in instance variables that I can  
access), and I don’t want to change the class any more than I have to.  
Can anyone offer any pointers on how to get some custom code executed  
when an instance of such a class is serialised and deserialised? What  
I’d like to do is, when I call YAML.dump(), and it sees an instance of  
the legacy class, to convert it into some data structure and dump that,  
and do the reverse on YAML.load(). I’m guessing I can do part of this  
with the to\_yaml() function, but I haven’t been able to find an adequate  
reference anywhere. I’d really appreciate any help anyone could give  
me. Thanks.
