Skip to content

Instantly share code, notes, and snippets.

@ineverov
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ineverov/3ce3ea21d970996b673e to your computer and use it in GitHub Desktop.
Save ineverov/3ce3ea21d970996b673e to your computer and use it in GitHub Desktop.
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