String.to_yaml newline confusion

Hi ruby-talk,

I ran into some weird behavior with to_yaml:

"{\"config_path\":\"something\",\"mode\":\"something_underscore\",\"person\":{\"id\":1,\"name\":\"Christopher Michael Thielen\",\"address\":\"First Shields Ave\"}}”.to_yaml

returns:

"--- '{\"config_path\":\"something\",\"mode\":\"something_underscore\",\"person\":{\"id\":1,\"name\":\"Christopher\n Michael Thielen\",\"address\":\"First Shields Ave\"}}'\n”

Note that a newline has been inserted after the word ‘Christopher’. It shouldn’t be there.

Curiously enough, if we leave out, say, the “mode” key, the newline shifts around:

"{\"config_path\":\"something\",\"person\":{\"id\":1,\"name\":\"Christopher Michael Thielen\",\"address\":\"First Shields Ave\"}}”.to_yaml

returns:

"--- '{\"config_path\":\"something\",\"person\":{\"id\":1,\"name\":\"Christopher Michael Thielen\",\"address\":\"First\n Shields Ave\"}}'\n”

Note that the newline isn’t after ‘Christopher' anymore but is after the word ‘First’.

This functionality appears in at least:

ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin12.5.0]

and

ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]

Is this a bug or am I missing something about the behavior of to_yaml?

Christopher Thielen
Lead Application Developer
Division of Social Science IT
University of California Davis

Can confirm:

"{\"config_path\":\"something\",\"mode\":\"something_underscore\",\"person\":{\"id\":1,\"name\":\"Christopher Michael Thielen\",\"address\":\"First Shields Ave\"}}".to_yaml.index("\n")

=> 98

"{\"config_path\":\"something\",\"person\":{\"id\":1,\"name\":\"Christopher Michael Thielen\",\"address\":\"First Shields Ave\"}}".to_yaml.index("\n")

=> 102

Looks like 100 might be a magic number of some sort. I'd certainly call this a bug. Esp since round tripping won't result in the same value as you started with (assuming, untested). :slight_smile:

P.S. Please don't use smart quotes in ruby samples. Makes it hard to copy & paste into IRB.

Me too, however, it does not seem to make any difference:

irb(main):004:0> puts YAML.load("{\"config_path\":\"something\",\"mode\":
\"something_underscore\",\"person\":{\"id\":1,\"name\":\"Christopher Michael
Thielen\",\"address\":\"First Shields Ave\"}}".to_yaml)

# => {"config_path":"something","mode":"something_underscore","person":
{"id":1,"name":"Christopher Michael Thielen","address":"First Shields Ave"}}

Do the quotes in YAML ignore newlines? I've found nothing in the specs, but I
only did a quick skimming.

      --- Eric

···

On Friday 30 January 2015, 17:10:44, Ryan Davis wrote:

Can confirm: