How can I get this backslash-eliminating behavior when reading afile?

Discussion in 'Ruby' started by Phil Tomson, Dec 31, 2007.

  1. Phil Tomson

    Phil Tomson Guest

    I'm trying to write a parser for a file format that allows backslashes
    '\' at the end of lines. The format I'm trying to parse treats
    backslashes at the end of lines just like the Ruby parser would treat
    them if they occured in a ruby source file.

    The double quotes string constructor does what I want it to in this case:

    irb(main):006:0> str = "line1 \
    irb(main):007:0" still more line1 \
    irb(main):008:0" even more line1"
    => "line1 still more line1 even more line1" <- this is exactly what I want


    However, if I read a string in from a file, I don't get what I want:
    $ cat testfile
    line 1 \
    still on line 1\
    even more line 1

    irb(main):001:0> str = File.read("testfile")
    => "line 1 \\\nstill on line 1\\\neven more line 1\n"

    I'd prefer not to even know about the backslashes or '\n's, so I'd
    rather have the behavior that the double quotes string constructor
    gives me:
    => "line1 still more line1 even more line1"

    Is there any way to get this behavior when reading a file into a string?

    Phil
     
    Phil Tomson, Dec 31, 2007
    #1
    1. Advertisements

  2. Phil Tomson

    Phil Tomson Guest

    On 12/30/07, Phil Tomson <> wrote:
    > I'm trying to write a parser for a file format that allows backslashes
    > '\' at the end of lines. The format I'm trying to parse treats
    > backslashes at the end of lines just like the Ruby parser would treat
    > them if they occured in a ruby source file.
    >
    > The double quotes string constructor does what I want it to in this case:
    >
    > irb(main):006:0> str = "line1 \
    > irb(main):007:0" still more line1 \
    > irb(main):008:0" even more line1"
    > => "line1 still more line1 even more line1" <- this is exactly what I want
    >
    >
    > However, if I read a string in from a file, I don't get what I want:
    > $ cat testfile
    > line 1 \
    > still on line 1\
    > even more line 1
    >
    > irb(main):001:0> str = File.read("testfile")
    > => "line 1 \\\nstill on line 1\\\neven more line 1\n"
    >
    > I'd prefer not to even know about the backslashes or '\n's, so I'd
    > rather have the behavior that the double quotes string constructor
    > gives me:
    > => "line1 still more line1 even more line1"
    >
    > Is there any way to get this behavior when reading a file into a string?
    >
    > Phil
    >


    ah, of course, I could just to this:
    str = File.read("testfile").gsub(/\\\n/,'')

    That does what I asked for... now I realize that I probably don't want
    to do this since the line numbering will be all off when reporting
    errors...

    Phil
     
    Phil Tomson, Dec 31, 2007
    #2
    1. Advertisements

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. Chuck Amadi
    Replies:
    0
    Views:
    645
    Chuck Amadi
    Jun 22, 2004
  2. Vincent Texier
    Replies:
    3
    Views:
    3,016
    Vincent Texier
    Jul 1, 2004
  3. Dravidan

    backslash in reading bytes

    Dravidan, Mar 20, 2008, in forum: Python
    Replies:
    1
    Views:
    342
    Marc 'BlackJack' Rintsch
    Mar 20, 2008
  4. Seid Mohammed
    Replies:
    0
    Views:
    359
    Seid Mohammed
    Nov 21, 2008
  5. Dean Holdren
    Replies:
    1
    Views:
    164
    Daniel Lucraft
    Jun 26, 2007
  6. Markus Fischer
    Replies:
    1
    Views:
    501
    Markus Fischer
    May 31, 2011
  7. alt.testing@{g}mail.com

    Double backslash behavior not as expected

    alt.testing@{g}mail.com, May 4, 2007, in forum: Perl Misc
    Replies:
    4
    Views:
    202
    Dan Mercer
    May 4, 2007
  8. Replies:
    8
    Views:
    292
    Dr.Ruud
    Jan 2, 2008
Loading...