Hi --

On Wed, 12 Nov 2003, 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
> 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


David

--
David Alan Black
home: [email]dblack@wobblini.net[/email] # New email address
work: [email]blackdav@shu.edu[/email]
Web: [url]http://pirate.shu.edu/~blackdav[/url]