Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Psych bug
require 'yaml'
YAML::ENGINE.yamler = 'psych'
puts "Custom encode_with and init_with"
class A
attr_reader :b
def initialize(b)
@b=b
end
end
class B
attr_reader :a
def initialize
@a=A.new(self)
end
#comment this
def encode_with(c)
c['a']=@a
end
#end comment
def init_with(c)
@a=c['a']
end
end
b = B.new
str= b.to_yaml
puts "YAML:"
puts str
b = YAML::load(str)
puts "References same? #{b.a.b.object_id == b.object_id}"
#Output is
#$ ruby 1.rb
#Custom encode_with and init_with
#YAML:
#--- !ruby/object:B
#a: &75276480 !ruby/object:A
# b: !ruby/object:B
# a: *75276480
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.