How to preserve line endings in strings with YAML?

T

Tom Payne

Hi all,

Normally I would expect that
YAML.load(x.to_yaml) == x
for all sensible values of x.

However, this does not seem to be the case if the string contains
certain new line characters, for example:
YAML.load("abc\r\n".to_yaml) # => "abc\n" ("\r" lost)
YAML.load("\r\n".to_yaml) # => "" ("\r\n" lost)
YAML.load("\n".to_yaml) # => "" ("\n" lost)

My question is: how can I configure YAML to preserve my line endings?
I'm using Ruby 1.8.4-preview2 and have Googled for a solution without
success. Note that the :UseBlock option to to_yaml does not help.

The reason that I need this functionality is that I would like to store
a string which includes a digital signature in a YAML document. Any
corruption of the line endings changes the string and consequently
invalidates the digital signature.

Thanks very much,
 
B

Bill Kelly

From: "Tom Payne said:
YAML.load("abc\r\n".to_yaml) # => "abc\n" ("\r" lost)
YAML.load("\r\n".to_yaml) # => "" ("\r\n" lost)
YAML.load("\n".to_yaml) # => "" ("\n" lost)

My question is: how can I configure YAML to preserve my line endings?
I'm using Ruby 1.8.4-preview2 and have Googled for a solution without
success. Note that the :UseBlock option to to_yaml does not help.
=> "\r\n"

But the third didn't:
=> " "

I guess if all else fails you could:
=> "\n"


Regards,

Bill
 
T

Tom Payne

Sorry to bump, but the timing of my initial posting (early hours of
Sunday morning European time, late Saturday US time, Sunday Japanese
time) was probably unwise.

Tom said:
Hi all,

Normally I would expect that
YAML.load(x.to_yaml) == x
for all sensible values of x.

However, this does not seem to be the case if the string contains
certain new line characters, for example:
YAML.load("abc\r\n".to_yaml) # => "abc\n" ("\r" lost)
YAML.load("\r\n".to_yaml) # => "" ("\r\n" lost)
YAML.load("\n".to_yaml) # => "" ("\n" lost)

My question is: how can I configure YAML to preserve my line endings?
I'm using Ruby 1.8.4-preview2 and have Googled for a solution without
success. Note that the :UseBlock option to to_yaml does not help.

The reason that I need this functionality is that I would like to store
a string which includes a digital signature in a YAML document. Any
corruption of the line endings changes the string and consequently
invalidates the digital signature.

Thanks very much,

No more bumps (promise!)

Cheers,
 
J

Johannes Friestad

That's part of the YAML specification.
To keep newlines, use the pipe syntax, like this:
---
signature: |
ax890a
b0098b

=3D> { 'signature' =3D> "ax890a\nb0098b\n" }
---
There are several other newline-keeping options..
Suggested reading:
http://yaml4r.sourceforge.net/cookbook/
Look under 'blocks'

cheers,
jf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top