How can I control which fields to serialize with YAML

ruby, yaml

Solution

In Ruby 1.9, `to_yaml_properties` is deprecated; if you're using Ruby 1.9, a more future proof method would be to use `encode_with`:

class Point
  def encode_with coder
    coder['x'] = @x
    coder['y'] = @y
  end
end

In this case that’s all you need, as the default is to set the corresponding instance variable of the new object to the appropriate value when loading from Yaml, but in more comple cases you could use `init_with`:

def init_with coder
  @x = coder['x']
  @y = coder['y']
end

Problem

For instance, ``` class Point attr_accessor :x, :y, :pointer_to_something_huge end ``` I only want to serialize x and y and leave everything else as nil.

Original source