[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Backslash substitution question

Ron Coutts

11/11/2003 10:22:00 PM

I'm having trouble with backslashes and I don't know what is wrong. I
can't seem to write and expression that will evaluate to a string
containing a single backslash character as in "\". It seems that "\\"
in Ruby evaluates to "\\" not to "\" as I would expect. Below are a few
things I've tried. If anyone could send back a quick answer it would be
much appreciated.

res = "c:/foo/bar".gsub(/\//, "\\") # -> c:\\foo\\bar
res = "c:/foo/bar".gsub(/\//, '\\') # -> c:\\foo\\bar
#res = "c:/foo/bar".gsub(/\//, "\") # -> syntax error - unterminated
string meets end of file
#res = "c:/foo/bar".gsub(/\//, '\') # -> syntax error - unterminated
string meets end of file
res = "c:/foo/bar".gsub(/\//) { "\\"} # -> c:\\foo\\bar

Ron


4 Answers

Hal E. Fulton

11/11/2003 10:26:00 PM

0

Ron Coutts wrote:
> I'm having trouble with backslashes and I don't know what is wrong. I
> can't seem to write and expression that will evaluate to a string
> containing a single backslash character as in "\". It seems that "\\"
> in Ruby evaluates to "\\" not to "\" as I would expect. Below are a few
> things I've tried. If anyone could send back a quick answer it would be
> much appreciated.
>
> res = "c:/foo/bar".gsub(/\//, "\\") # -> c:\\foo\\bar

This one works. You're getting confused by either irb
or by calling p. You're seeing the escaped form, but it's
not stored that way internally.

Note that res.length is 10, not 12.

Hal


Eric Hodel

11/11/2003 10:28:00 PM

0

Ron Coutts (rcoutts@envistatech.com) wrote:

> I'm having trouble with backslashes and I don't know what is wrong. I
> can't seem to write and expression that will evaluate to a string
> containing a single backslash character as in "\". It seems that "\\"
> in Ruby evaluates to "\\" not to "\" as I would expect. Below are a few
> things I've tried. If anyone could send back a quick answer it would be
> much appreciated.
>
> res = "c:/foo/bar".gsub(/\//, "\\") # -> c:\\foo\\bar
> res = "c:/foo/bar".gsub(/\//, '\\') # -> c:\\foo\\bar
> #res = "c:/foo/bar".gsub(/\//, "\") # -> syntax error - unterminated
> string meets end of file
> #res = "c:/foo/bar".gsub(/\//, '\') # -> syntax error - unterminated
> string meets end of file
> res = "c:/foo/bar".gsub(/\//) { "\\"} # -> c:\\foo\\bar

Ruby maps between / and \ in filenames on win32 just fine, why not let
it do the work for you? For cmd.exe, it doesn't cane if you give it or /, but tab completion doesn't work on /. (I believe I read somewhere
that the guts don't care, since back in the DOS days, but my mind could
be addled).

See also File.join and File.expand_path

--
Eric Hodel - drbrain@segment7.net - http://se...
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Ryan Pavlik

11/11/2003 10:30:00 PM

0

On Wed, 12 Nov 2003 07:21:51 +0900
"Ron Coutts" <rcoutts@envistatech.com> wrote:

> I'm having trouble with backslashes and I don't know what is wrong. I
> can't seem to write and expression that will evaluate to a string
> containing a single backslash character as in "\". It seems that "\\"
> in Ruby evaluates to "\\" not to "\" as I would expect. Below are a few
> things I've tried. If anyone could send back a quick answer it would be
> much appreciated.

In any ruby string literal, backslash escapes the next character.
This is so you can write:

puts "\"hello world\"" # => "hello world"

It escapes itself, too, so you have a way of printing backslashes:

puts "\\" # =>
It works inside single quotes, so you can escape single quotes:

puts '\'' # => '

That means it needs to escape itself as well, otherwise you couldn't
print a backslash:

puts '\\' # =>
This also works inside docstrings.

hth,

--
Ryan Pavlik <rpav@mephle.com>

"*More* hapless visitors? Tsk, and I'm all out of doom quests." - 8BT

Ryan Pavlik

11/11/2003 10:36:00 PM

0

On Wed, 12 Nov 2003 07:30:08 +0900
Ryan Pavlik <rpav@mephle.com> wrote:

<basic obvious stuff>

Sorry, after seeing others post, I realize I misunderstood your
question. Yeah, what they said. Always test with puts/print. ;-)

--
Ryan Pavlik <rpav@mephle.com>

"*More* hapless visitors? Tsk, and I'm all out of doom quests." - 8BT