Forum: Ruby Problem with YAML not returning exactly the same

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
732234b837d70468f72d571d2f439094?d=identicon&s=25 Oliver Burghard (Guest)
on 2008-11-03 11:24
(Received via mailing list)
Attachment: marshal (4 KB)
Hey,

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
attached.

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

==================================================
require 'yaml'
require 'pp'

# read the data with marshal
data1 = nil
File.open( "marshal", "r") {|file|
  data1 = Marshal.load( file )
}

# write and read with yaml
data2 = YAML.load data1.to_yaml
puts data1 == data2  # => false

# put differences
File.open( "data1", "w") { |file|
  PP.pp data1, file
}
File.open( "data2", "w") { |file|
  PP.pp( data2, file )
}

system "diff data1 data2"
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-11-03 15: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.
http://www.ruby-lang.org/en/community/ruby-core/#p...
This topic is locked and can not be replied to.