Is possible Ruby to use CRLF instead of LF?

Hi, AFAIK Ruby uses LF = \n to detect newline. But now I’m coding a
parser for a protocol that uses CRLF = \r\n for newline. In fact, \n
is not considered a newline.

Is it possible to get Ruby working with CRLF instead of LF?

For example, I want to declare this string (for testing):

example =<-- END_STRING
Version 4
Request_Type: call
From: sssss
END_STRING

and I want that string to match \r\n at the end of each line instead
of \n, is it possible?

PD: Also it would be great if I could enter \r\n in a Linux telnet
isntead of just \n for amnual testing, but assume it’s not appropiate
question in this maillist :wink:

Thanks in advance.

To change \n to \r\n you can use:

a.insert(index(“\n”),“\r”)

It will put \r before \n but only once. If there is more than one \n in
text
you may use it in loop.

2008/3/25, Iñaki Baz C. [email protected]:

2008/3/25, Mateusz T. [email protected]:

To change \n to \r\n you can use:

a.insert(index(“\n”),“\r”)

It will put \r before \n but only once. If there is more than one \n in text
you may use it in loop.

Ok, I’ll try it. Thanks.

El Martes, 25 de Marzo de 2008, Gerardo S. Gómez Garrido
escribió:> > Version 4

Request_Type: call
From: sssss
END_STRING

and I want that string to match \r\n at the end of each line instead
of \n, is it possible?

Well, you could type that text in Notepad and then copy/pasting the
text to your code. Or even better, use unix2dos.

Hi, finally I did it (note also that I don’t use Windows :wink: ):

example =<-- END_STRING
Version 4
Request_Type: call
From: sssss
END_STRING

example.gsub!(/\n/,"\r\n")

XD

Thanks a lot.

On Mar 25, 2008, at 17:50 , Iñaki Baz C. wrote:

Request_Type: call
From: sssss
END_STRING

and I want that string to match \r\n at the end of each line instead
of \n, is it possible?

The whole question is a bit generic.

In the source code a hard-newline like the one in a here-document has
only LFs as long as the file has the newline conventions of the
runtime platform. That’s because the Ruby interpreter itself reads the
program as a text file in text mode.

If you want to force CRLF in a here-document you can use a trick like
this (off the top of my head):

example = <<EOS.lf_to_crlf

EOS

class String
def lf_to_crlf
gsub(/\012/, “\015\012”)
end
end

Then the I/O channel used to send the data needs to be in binary mode,
etc.

– fxn

El Martes, 25 de Marzo de 2008, Xavier N.
escribió:

EOS

class String
def lf_to_crlf
gsub(/\012/, “\015\012”)
end
end

Then the I/O channel used to send the data needs to be in binary mode,
etc.

Thanks, finally that it what I did (similar):

example =<-- END_STRING
Version 4
Request_Type: call
From: sssss
END_STRING

example.gsub!(/\n/,"\r\n")

Thanks a lot.

On 3/25/08, Iñaki Baz C. [email protected] wrote:

Request_Type: call
From: sssss
END_STRING

and I want that string to match \r\n at the end of each line instead
of \n, is it possible?

Well, you could type that text in Notepad and then copy/pasting the
text to your code. Or even better, use unix2dos.

PD: Also it would be great if I could enter \r\n in a Linux telnet
isntead of just \n for amnual testing, but assume it’s not appropiate
question in this maillist :wink:

You know, you can always redirect the input. Instead of typing the
lines, store them in a text file, convert them with unix2dos and then
feed the file to your program:

yourprog < textfile

On Mar 26, 2008, at 17:39 , mrhassell wrote:

The dot matches a single char, no care 4 what the character is. The
exception is newline characters. In regex things, the dot will ‘not
match’ a newline character by default. So by default, the dot is short
for the negated character class [^\n] (UNIX regex flavors) or [^\r\n]
(Windows regex flavors)…

Careful, the dot does match \r. The dot is exactly [^\n] (modulus /m).

What happens is that in ordinary line-oriented, portable code, you
don’t see a single \r in Ruby strings. The CRs on Windows are on the
disk but don’t go up because the I/O layer filters them.

– fxn

Regular Expressions = flexible + fast!

myarray = mystring.scan(/regex/)

“[^”\r\n]*"

The dot matches a single char, no care 4 what the character is. The
exception is newline characters. In regex things, the dot will ‘not
match’ a newline character by default. So by default, the dot is short
for the negated character class [^\n] (UNIX regex flavors) or [^\r\n]
(Windows regex flavors)… In RegexBuddy, EditPad Pro or PowerGREP,
you tick the checkbox labeled “dot matches newline” when building your
expressions…

wtf… ladies… wtf! of course anything is possible or we wouldn’t
stand a chance…

:slight_smile:

El Martes, 25 de Marzo de 2008, Iñaki Baz C. escribió:

Request_Type: call
From: sssss
END_STRING

and I want that string to match \r\n at the end of each line instead
of \n, is it possible?

Ok, using io.gets or io.readline I can set as parameter the lines
separator
string:

ios.gets(sep_string=$/)

And in Ruby by default (at least in Linux) variable $/ is \n. But I can
set
this variable:
$/ = “\r\n”
so I assume it would work.

Iñaki Baz C. wrote:

Request_Type: call

Thanks in advance.

As a newbee regarding the development in ruby I am somewhat surprised
that the CRLF issue is not covered by something like a ruby “pragma”.

With perl I am used to write something like:

use open IN => “:crlf”, OUT => “:bytes”;
use open IN => “:bytes”, OUT => “:crlf”;
use open OUT => ‘:utf8’;
… and so on …

this “pragma” specifies how IO is generally to be handled.

don’t we have a comparable syntax ?