Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How can I get this backslash-eliminating behavior when reading afile?

Reply
Thread Tools

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

 
 
Phil Tomson
Guest
Posts: n/a
 
      12-31-2007
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

 
Reply With Quote
 
 
 
 
Phil Tomson
Guest
Posts: n/a
 
      12-31-2007
On 12/30/07, Phil Tomson <(E-Mail Removed)> 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

 
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
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM
Javascript can get time, can it get milliseconds, or actually just tenths of seconds? Guy Javascript 2 12-05-2003 04:00 PM
Strange behavior after reading big file J Krugman Perl Misc 3 09-06-2003 11:28 PM



Advertisments