Problem with YAML not returning exactly the same


I have some data that I output with YAML and read back in. I use these
for hashing. So It would be necessary that the read value is exactly the
same as the outputted value. But this is sometimes not the case.

The data is complex as it is a hash with an array of strings as keys and
an array of strings as values.

Now I wonder if this is an error or just my misunderstanding of YAML.

Here is the source code reproducing the error with the data file

I am using Marshal now again, as in my case it reproduces the data

require ‘yaml’
require ‘pp’

read the data with marshal

data1 = nil “marshal”, “r”) {|file|
data1 = Marshal.load( file )

write and read with yaml

data2 = YAML.load data1.to_yaml
puts data1 == data2 # => false

put differences “data1”, “w”) { |file|
PP.pp data1, file
} “data2”, “w”) { |file|
PP.pp( data2, file )

system “diff data1 data2”

Yes, I can reproduce this here with both 1.8.4 and 1.9.1-preview1.

YAML is fine for reading config files, but as you’ve just discovered, it
sucks for object serialisation. Welcome to YAML.

Your test case appears to boil down to the following:

require ‘yaml’
data1 = [{“foo”=>"\n", “bar”=>“baz\n \nbaz”}]
p data1
data2 = YAML.load data1.to_yaml
p data2

This gives the following results:

[{“foo”=>"\n", “bar”=>“baz\n \nbaz”}]
[{“foo”=>"", “bar”=>“baz\n\nbaz”}]

Feel free to submit it as a bug report.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs