Last active
August 29, 2015 14:09
-
-
Save ineverov/3ce3ea21d970996b673e to your computer and use it in GitHub Desktop.
Psych bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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