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
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.
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).
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
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
Are you doing this in irb? If so, you’ll get a quoted and escaped
version of the new string which shows you \ where the string actually
has \
irb(main):005:0> res = “c:/foo/bar”.gsub(///, “\”)
=> “c:\foo\bar”
irb(main):006:0> puts res
c:\foo\bar
irb(main):007:0> res.size
=> 10
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.
Thanks for all the help everybody and the quick turnaround, you’ve saved
me some valuable time. I wasn’t aware of the ‘quoted and escaped’
versions of these strings.
To reply to Eric, usually on win32 I just use ‘/’ and let Ruby handle it
for me but the script I’m working on now has to enter this string into a
Windows GUI app edit field and that app doesn’t like ‘/’, that’s why I
had to convert it myself.
Hal, what did you mean by ‘calling p’?
Thanks again,
Ron
···
-----Original Message-----
From: Hal Fulton [mailto:hal9000@hypermetrics.com]
Sent: November 11, 2003 3:26 PM
To: ruby-talk ML
Subject: Re: Backslash substitution question
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.