From: "J. Cooper" Date: 2008-02-23T02:37:49+09:00 Subject: Re: gsub("\\", "\\\\") seems unintuitive John Woods wrote: > The following confusing behavior is noted in the pickaxe book (2nd ed) > on page 75: > > # I would expect two backslashes in the result > irb> puts "\\".gsub("\\","\\\\") > \ > > # I would expect four backslashes in the result > irb> puts "\\".gsub("\\","\\\\\\\\") > \\ > > I can certainly work around it, but it seems unintuitive. Is there a > reason why gsub behaves this way? Just curious... It's not a gsub thing, per se--it's a string thing. Backslashes are used in strings to escape special characters. One such character is a " mark. If you want to write a " mark in the middle of a string, you have to escape it with a backslash: "They call me \"Mellow Yellow\" etc." If you didn't, then the " would signify the end of the string! Similarly, in the example you listed, if you just did: "\" then you end up with a string that ISN'T ended! Because you escaped the next ". So, if you want a literal backslash, you have to escape the backslash too: "\\" It just looks confusing because you are escaping the escape character :) -- Posted via http://www.ruby-forum.com/.