[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Is possible Ruby to use CRLF instead of LF?

Iñaki Baz Castillo

3/25/2008 4:51:00 PM

SGksIEFGQUlLIFJ1YnkgdXNlcyBMRiA9IFxuIHRvIGRldGVjdCBuZXdsaW5lLiBCdXQgbm93IEkn
bSBjb2RpbmcgYQpwYXJzZXIgZm9yIGEgcHJvdG9jb2wgdGhhdCB1c2VzIENSTEYgPSBcclxuIGZv
ciBuZXdsaW5lLiBJbiBmYWN0LCBcbgppcyBub3QgY29uc2lkZXJlZCBhIG5ld2xpbmUuCgpJcyBp
dCBwb3NzaWJsZSB0byBnZXQgUnVieSB3b3JraW5nIHdpdGggQ1JMRiBpbnN0ZWFkIG9mIExGPwoK
Rm9yIGV4YW1wbGUsIEkgd2FudCB0byBkZWNsYXJlIHRoaXMgc3RyaW5nIChmb3IgdGVzdGluZyk6
CgpleGFtcGxlID08LS0gRU5EX1NUUklORwpWZXJzaW9uIDQKUmVxdWVzdF9UeXBlOiBjYWxsCkZy
b206IHNzc3NzCkVORF9TVFJJTkcKCmFuZCBJIHdhbnQgdGhhdCBzdHJpbmcgdG8gbWF0Y2ggXHJc
biBhdCB0aGUgZW5kIG9mIGVhY2ggbGluZSBpbnN0ZWFkCm9mIFxuLCBpcyBpdCBwb3NzaWJsZT8K
ClBEOiBBbHNvIGl0IHdvdWxkIGJlIGdyZWF0IGlmIEkgY291bGQgZW50ZXIgXHJcbiBpbiBhIExp
bnV4IHRlbG5ldAppc250ZWFkIG9mIGp1c3QgXG4gZm9yIGFtbnVhbCB0ZXN0aW5nLCBidXQgYXNz
dW1lIGl0J3Mgbm90IGFwcHJvcGlhdGUKcXVlc3Rpb24gaW4gdGhpcyBtYWlsbGlzdCA7KQoKClRo
YW5rcyBpbiBhZHZhbmNlLgoKCi0tIApJw7Fha2kgQmF6IENhc3RpbGxvCjxpYmNAYWxpYXgubmV0
Pgo=

10 Answers

WujcioL

3/25/2008 5:05:00 PM

0

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 tex=
t
you may use it in loop.

2008/3/25, I=F1aki Baz Castillo <ibc@aliax.net>:
>
> Hi, AFAIK Ruby uses LF =3D \n to detect newline. But now I'm coding a
> parser for a protocol that uses CRLF =3D \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 =3D<-- 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 ;)
>
>
> Thanks in advance.
>
>
>
> --
> I=F1aki Baz Castillo
> <ibc@aliax.net>
>

Iñaki Baz Castillo

3/25/2008 5:16:00 PM

0

MjAwOC8zLzI1LCBNYXRldXN6IFR5YnVyYSA8d3VqY2lvbEBnbWFpbC5jb20+Ogo+IFRvIGNoYW5n
ZSBcbiB0byBcclxuIHlvdSBjYW4gdXNlOgo+Cj4gIGEuaW5zZXJ0KGluZGV4KCJcbiIpLCJcciIp
Cj4KPiAgSXQgd2lsbCBwdXQgXHIgYmVmb3JlIFxuIGJ1dCBvbmx5IG9uY2UuIElmIHRoZXJlIGlz
IG1vcmUgdGhhbiBvbmUgXG4gaW4gdGV4dAo+ICB5b3UgbWF5IHVzZSBpdCBpbiBsb29wLgoKT2ss
IEknbGwgdHJ5IGl0LiBUaGFua3MuCgotLSAKScOxYWtpIEJheiBDYXN0aWxsbwo8aWJjQGFsaWF4
Lm5ldD4K

Gerardo Santana Gómez Garrido

3/25/2008 7:12:00 PM

0

On 3/25/08, I=F1aki Baz Castillo <ibc@aliax.net> wrote:
> Hi, AFAIK Ruby uses LF =3D \n to detect newline. But now I'm coding a
> parser for a protocol that uses CRLF =3D \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 =3D<-- 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?

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 ;)

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


--=20
Gerardo Santana

Iñaki Baz Castillo

3/25/2008 7:55:00 PM

0

El Martes, 25 de Marzo de 2008, Gerardo Santana G=F3mez Garrido escribi=F3:
> On 3/25/08, I=F1aki Baz Castillo <ibc@aliax.net> wrote:
> > Hi, AFAIK Ruby uses LF =3D \n to detect newline. But now I'm coding a
> > parser for a protocol that uses CRLF =3D \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 =3D<-- 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?
>
> 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 ;) ):

example =3D<-- END_STRING
Version 4
Request_Type: call
=46rom: sssss
END_STRING

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


XD



Thanks a lot.

=2D-=20
I=F1aki Baz Castillo

Xavier Noria

3/25/2008 8:03:00 PM

0

On Mar 25, 2008, at 17:50 , I=F1aki Baz Castillo wrote:

> Hi, AFAIK Ruby uses LF =3D \n to detect newline. But now I'm coding a
> parser for a protocol that uses CRLF =3D \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 =3D<-- 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?

The whole question is a bit generic.

In the source code a hard-newline like the one in a here-document has =20=

only LFs as long as the file has the newline conventions of the =20
runtime platform. That's because the Ruby interpreter itself reads the =20=

program as a text file in text mode.

If you want to force CRLF in a here-document you can use a trick like =20=

this (off the top of my head):

example =3D <<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, =20=

etc.

-- fxn


Iñaki Baz Castillo

3/25/2008 10:03:00 PM

0

El Martes, 25 de Marzo de 2008, Xavier Noria escribi=F3:

> 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 =3D <<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.

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


example =3D<-- END_STRING
Version 4
Request_Type: call
=46rom: sssss
END_STRING

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



Thanks a lot.



=2D-=20
I=F1aki Baz Castillo

mrhassell

3/26/2008 4:38:00 PM

0

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...

:-)

Xavier Noria

3/26/2008 4:59:00 PM

0

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




John Smith

3/27/2008 10:50:00 PM

0

Iñaki Baz Castillo wrote:
> 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 ;)
>
>
> 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 ?

Iñaki Baz Castillo

3/27/2008 11:04:00 PM

0

El Martes, 25 de Marzo de 2008, I=C3=B1aki Baz Castillo escribi=C3=B3:
> Hi, AFAIK Ruby uses LF =3D \n to detect newline. But now I'm coding a
> parser for a protocol that uses CRLF =3D \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 =3D<-- 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?


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

ios.gets(sep_string=3D$/)

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


=2D-=20
I=C3=B1aki Baz Castillo