Forum: Ruby How to preserve line endings in strings with YAML?

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.
Tom P. (Guest)
on 2005-12-11 03:23
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,
--
Tom
billk (Guest)
on 2005-12-11 04:12
(Received via mailing list)
From: "Tom P." <removed_email_address@domain.invalid>
>
>  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.

On my system, ruby 1.8.2 (2004-12-25) [i386-mswin32], the first two
seemed to work:

>> YAML.load("abc\r\n".to_yaml)
=> "abc\r\n"
>> YAML.load("\r\n".to_yaml)
=> "\r\n"

But the third didn't:

>> YAML.load("\n".to_yaml)
=> " "

I guess if all else fails you could:

>> CGI.unescape(YAML.load(CGI.escape("\n").to_yaml))
=> "\n"


Regards,

Bill
Tom P. (Guest)
on 2005-12-14 00:53
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 P. wrote:
> 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,
> --
> Tom

No more bumps (promise!)

Cheers,
--
Tom
Johannes F. (Guest)
on 2005-12-14 07:52
(Received via mailing list)
That's part of the YAML specification.
To keep newlines, use the pipe syntax, like this:
---
signature: |
   ax890a
   b0098b

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

cheers,
jf
Johannes F. (Guest)
on 2005-12-14 08:04
(Received via mailing list)
BTW: If you are storing binary data, you would need to convert it to
Base64 to get printable characters only (and handle the newlines
yourself).

Here's an example from the spec (http://yaml.org/spec/current.html )
---
picture: |
 R0lGODlhDAAMAIQAAP//9/X
 17unp5WZmZgAAAOfn515eXv
 Pz7Y6OjuDg4J+fn5OTk6enp
 56enmleECcgggoBADs=
---

jf

On 12/13/05, Johannes F. <removed_email_address@domain.invalid> wrote:
> Suggested reading:
> http://yaml4r.sourceforge.net/cookbook/
> Look under 'blocks'
This topic is locked and can not be replied to.