From: Tanaka Akira Date: 2003-06-06T18:14:48+09:00 Subject: Re: YAML problem In article <20030605060109.GA60067@rysa.inetz.com>, why the lucky stiff writes: > Fixed. Was a problem with anchors on a collection. Thank you. But I found YAML has more problem. % ruby -ryaml -e 'o = "\0"; p YAML.load(o.to_yaml)' "\\z" The result "\\z" is not equal to the input "\0". I found above problem by following simple test program which generates random objects. It seems that YAML has more problems. % cat tst #!/usr/bin/env ruby class Hash def hash h = 0 self.each {|k,v| h ^= k.hash ^ v.hash } h end alias eql? == end def random_fixnum rand(0x80000000) - 0x40000000 end def random_bool [false, true][rand(2)] end def random_string len = rand(4) chars = [] (0...len).each {|i| ## don't check bytes with MSB. #chars << rand(256) chars << rand(128) } chars.pack("C*") end def random_array o = [] rand(4).times { o << random_obj } o end def random_hash o = {} rand(4).times { o[random_obj] = random_obj } o end Simple = [ :random_fixnum, :random_bool, :random_string, ] Complex = [ :random_array, :random_hash, ] Both = Simple + Complex NestLimit = 10 Thread.current[:nest] = 0 def random_obj if Thread.current[:nest] < NestLimit methods = Both else methods = Simple end begin Thread.current[:nest] += 1 m = methods[rand(methods.length)] self.send m ensure Thread.current[:nest] -= 1 end end require 'yaml' require 'pp' 100.times { o0 = random_obj begin y = o0.to_yaml o1 = YAML.load(y) raise "not equal" unless o0 == o1 rescue pp o0 pp o1 puts y raise end } % ./tst [{false=>266179502, 334540002=>"\n\022\022"}, {false=>true, 1055494844=>103879957}, true] [{334540002=>"\\n\\x12\\x12", false=>266179502}, {false=>true, 1055494844=>103879957}, true] - false: 266179502 334540002: >- \n\x12\x12 - false: true 1055494844: 103879957 - true ./tst:89: not equal (RuntimeError) from ./tst:84:in `times' from ./tst:84 -- Tanaka Akira