From: dblack@... Date: 2003-11-12T07:29:17+09:00 Subject: Re: Backslash substitution question 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: dblack@wobblini.net # New email address work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav