Ralph Shnelvar wrote in post #962847:
> Consider the string
>   \1\2\3
> that is
>   "\\1\\2\\3"
>
> I feel really stupid ... but this simple substitution pattern does not
> do what I expect.
>
>   "\\1\\2\\3".gsub(/\\/,"\\\\")

Here you are replacing one backslash with one backslash.

The trouble is, in the *replacement* string, '\1' has a special meaning 
(insert the value of the first capture). Because of this, a literal 
backslash is backslash-backslash.

So to replace with *two* backslashes you need 
backslash-backslash-backslash-backslash. And inside a double or single 
quoted string, a single backslash is represented as "\\" or '\\'

irb(main):001:0> "\\1\\2\\3".gsub(/\\/,"\\\\\\\\")
=> "\\\\1\\\\2\\\\3"

The second level of backslashing isn't used with the block form, since 
if you want to use captured subexpressions you can use #{$1} instead of 
\1. Hence as an alternative:

irb(main):002:0> "\\1\\2\\3".gsub(/\\/) { "\\\\" }
=> "\\\\1\\\\2\\\\3"

-- 
Posted via http://www.ruby-forum.com/.