# Problem with YAML not returning exactly the same

**URL:** <https://www.ruby-forum.com/t/problem-with-yaml-not-returning-exactly-the-same/152377>\
**Category:** Ruby\
**Created:** [November 3, 2008, 11:24am UTC](https://www.ruby-forum.com/t/problem-with-yaml-not-returning-exactly-the-same/152377 "2008-11-03T11:24:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ol1barn3s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@ol1barn3s](https://www.ruby-forum.com/u/ol1barn3s)\
**Post date:** [November 3, 2008, 11:24am UTC](https://www.ruby-forum.com/t/problem-with-yaml-not-returning-exactly-the-same/152377/1 "2008-11-03T11:24:07Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![ol1barn3s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@ol1barn3s](https://www.ruby-forum.com/u/ol1barn3s)\
**Post date:** [November 3, 2008, 3:02pm UTC](https://www.ruby-forum.com/t/problem-with-yaml-not-returning-exactly-the-same/152377/2 "2008-11-03T15:02:46Z")

</div>

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/#patching-ruby](http://www.ruby-lang.org/en/community/ruby-core/#patching-ruby)
