Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to preserve line endings in strings with YAML?

Reply
Thread Tools

How to preserve line endings in strings with YAML?

 
 
Tom Payne
Guest
Posts: n/a
 
      12-11-2005
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

--
Posted via http://www.ruby-forum.com/.


 
Reply With Quote
 
 
 
 
Bill Kelly
Guest
Posts: n/a
 
      12-11-2005
From: "Tom Payne" <(E-Mail Removed)>
>
> 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




 
Reply With Quote
 
 
 
 
Tom Payne
Guest
Posts: n/a
 
      12-13-2005
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 Payne 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

--
Posted via http://www.ruby-forum.com/.


 
Reply With Quote
 
Johannes Friestad
Guest
Posts: n/a
 
      12-14-2005
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

On 12/13/05, Tom Payne <(E-Mail Removed)> wrote:
> 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 Payne wrote:
> > Hi all,
> >
> > Normally I would expect that
> > YAML.load(x.to_yaml) =3D=3D 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) # =3D> "abc\n" ("\r" lost)
> > YAML.load("\r\n".to_yaml) # =3D> "" ("\r\n" lost)
> > YAML.load("\n".to_yaml) # =3D> "" ("\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
>
> --
> Posted via http://www.ruby-forum.com/.
>
>



--
Johannes Friestad
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Printing unix Line endings from Windows. Ant Python 6 12-05-2006 03:58 PM
Detecting line endings Fuzzyman Python 18 02-08-2006 12:10 PM
UTF16, BOM, and Windows Line endings Fuzzyman Python 4 02-07-2006 09:23 AM
FileUtils - :preserve does not preserve mtime of directories on Windoze Grzegorz Chrupala Ruby 2 06-30-2003 07:38 AM



Advertisments