Skip to content

Instantly share code, notes, and snippets.

@rewinfrey
Created December 12, 2012 19:35
Show Gist options
  • Save rewinfrey/4270841 to your computer and use it in GitHub Desktop.
Save rewinfrey/4270841 to your computer and use it in GitHub Desktop.
A simple example showing how attributes of objects that are objects are also serialized when we serialize the base object
require 'yaml'
class RetainAttributeOfToYaml
attr_accessor :name
def initialize(name)
@name = "Hi, my name is #{name}."
end
end
class ToYaml
attr_accessor :attribute_to_retain
def initialize(attribute)
@attribute_to_retain = attribute
end
end
attribute1 = RetainAttributeOfToYaml.new("Schrodinger")
attribute2 = RetainAttributeOfToYaml.new("Cat")
ex1 = ToYaml.new(attribute1)
ex2 = ToYaml.new(attribute2)
ex1_serialized = YAML::dump(ex1)
ex2_serialized = YAML::dump(ex2)
ex1_deserialized = YAML::load(ex1_serialized)
ex2_deserialized = YAML::load(ex2_serialized)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment