Is possible Ruby to use CRLF instead of LF?

Discussion in 'Ruby' started by Iñaki Baz Castillo, Mar 25, 2008.

  1. SGksIEFGQUlLIFJ1YnkgdXNlcyBMRiA9IFxuIHRvIGRldGVjdCBuZXdsaW5lLiBCdXQgbm93IEkn
    bSBjb2RpbmcgYQpwYXJzZXIgZm9yIGEgcHJvdG9jb2wgdGhhdCB1c2VzIENSTEYgPSBcclxuIGZv
    ciBuZXdsaW5lLiBJbiBmYWN0LCBcbgppcyBub3QgY29uc2lkZXJlZCBhIG5ld2xpbmUuCgpJcyBp
    dCBwb3NzaWJsZSB0byBnZXQgUnVieSB3b3JraW5nIHdpdGggQ1JMRiBpbnN0ZWFkIG9mIExGPwoK
    Rm9yIGV4YW1wbGUsIEkgd2FudCB0byBkZWNsYXJlIHRoaXMgc3RyaW5nIChmb3IgdGVzdGluZyk6
    CgpleGFtcGxlID08LS0gRU5EX1NUUklORwpWZXJzaW9uIDQKUmVxdWVzdF9UeXBlOiBjYWxsCkZy
    b206IHNzc3NzCkVORF9TVFJJTkcKCmFuZCBJIHdhbnQgdGhhdCBzdHJpbmcgdG8gbWF0Y2ggXHJc
    biBhdCB0aGUgZW5kIG9mIGVhY2ggbGluZSBpbnN0ZWFkCm9mIFxuLCBpcyBpdCBwb3NzaWJsZT8K
    ClBEOiBBbHNvIGl0IHdvdWxkIGJlIGdyZWF0IGlmIEkgY291bGQgZW50ZXIgXHJcbiBpbiBhIExp
    bnV4IHRlbG5ldAppc250ZWFkIG9mIGp1c3QgXG4gZm9yIGFtbnVhbCB0ZXN0aW5nLCBidXQgYXNz
    dW1lIGl0J3Mgbm90IGFwcHJvcGlhdGUKcXVlc3Rpb24gaW4gdGhpcyBtYWlsbGlzdCA7KQoKClRo
    YW5rcyBpbiBhZHZhbmNlLgoKCi0tIApJw7Fha2kgQmF6IENhc3RpbGxvCjxpYmNAYWxpYXgubmV0
    Pgo=
     
    Iñaki Baz Castillo, Mar 25, 2008
    #1
    1. Advertising

  2. 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 <>:
    >
    > 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
    > <>
    >
     
    Mateusz Tybura, Mar 25, 2008
    #2
    1. Advertising

  3. MjAwOC8zLzI1LCBNYXRldXN6IFR5YnVyYSA8d3VqY2lvbEBnbWFpbC5jb20+Ogo+IFRvIGNoYW5n
    ZSBcbiB0byBcclxuIHlvdSBjYW4gdXNlOgo+Cj4gIGEuaW5zZXJ0KGluZGV4KCJcbiIpLCJcciIp
    Cj4KPiAgSXQgd2lsbCBwdXQgXHIgYmVmb3JlIFxuIGJ1dCBvbmx5IG9uY2UuIElmIHRoZXJlIGlz
    IG1vcmUgdGhhbiBvbmUgXG4gaW4gdGV4dAo+ICB5b3UgbWF5IHVzZSBpdCBpbiBsb29wLgoKT2ss
    IEknbGwgdHJ5IGl0LiBUaGFua3MuCgotLSAKScOxYWtpIEJheiBDYXN0aWxsbwo8aWJjQGFsaWF4
    Lm5ldD4K
     
    Iñaki Baz Castillo, Mar 25, 2008
    #3
  4. On 3/25/08, 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?


    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
     
    Gerardo Santana Gómez Garrido, Mar 25, 2008
    #4
  5. El Martes, 25 de Marzo de 2008, Gerardo Santana G=F3mez Garrido escribi=F3:
    > On 3/25/08, 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?

    >
    > 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
     
    Iñaki Baz Castillo, Mar 25, 2008
    #5
  6. Iñaki Baz Castillo

    Xavier Noria Guest

    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
     
    Xavier Noria, Mar 25, 2008
    #6
  7. 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
     
    Iñaki Baz Castillo, Mar 25, 2008
    #7
  8. Iñaki Baz Castillo

    Xavier Noria Guest

    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
     
    Xavier Noria, Mar 26, 2008
    #8
  9. Iñaki Baz Castillo

    John Smith Guest

    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 ?
     
    John Smith, Mar 27, 2008
    #9
  10. 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
     
    Iñaki Baz Castillo, Mar 27, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry III

    Re: how to check CRLF in C#

    Jerry III, Aug 25, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    813
    Jerry III
    Aug 25, 2003
  2. Kevin Spencer

    Re: how to check CRLF in C#

    Kevin Spencer, Aug 25, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    27,960
    Jerry III
    Aug 27, 2003
  3. Peter Hogoven
    Replies:
    0
    Views:
    393
    Peter Hogoven
    Sep 8, 2004
  4. Alexis
    Replies:
    3
    Views:
    614
    Eliyahu Goldin
    Dec 13, 2007
  5. Wai Tsang
    Replies:
    11
    Views:
    193
    Wai Tsang
    Apr 26, 2007
Loading...

Share This Page