Forum: Ruby Problem with YAML not returning exactly the same

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Oliver B. (Guest)
on 2008-11-03 12:24
(Received via mailing list)
Attachment: marshal (0 Bytes)

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"
Brian C. (Guest)
on 2008-11-03 16:02
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 topic is locked and can not be replied to.