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

Advertisements

~ 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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: