to_yaml_properties

There are just so many little secrets in Ruby. Today we found one. We have a class and we want to be able to serialize it using to_yaml, but we just want to store some, not all, instance variables. After 10 minutes digging out, we found all we need is overwrite to_yaml_properties method, made it return an array of instance variables we want. Like this:


def to_yaml_properties
["@attr1", "@attr2"]
end

~ by Yi Wen on February 20, 2008.

2 Responses to “to_yaml_properties”

  1. I too spent a lot of time on a similar quest. I found that the instance names have to be named:

    ["@attr1", "@attr2"]

  2. Thanks, Alla n, you are right. It’s a mistake in my post. I will fix it.

Leave a Reply