From: Kornelius Kalnbach Date: 2010-04-15T07:16:50+09:00 Subject: [ruby-core:29524] Re: [Bug #3147] gsub evaluates the passed in replacement string making results unpredictable when replacement string is dynamically generated I use the pattern str.gsub(pattern) { replacement } when I'm not sure about the replacement. In this case: 'Y'.gsub(/(Y)/) { '\\' + $1 } This specific example could also be written as: 'Y'.gsub(/(?=Y)/, '\\') Both avoid the backslash issue. Escaping rules in replacement strings are indeed confusing. If you want a single backslash in the output, you need four of them in the literal. Adding Wayne's examples to the documentation would certainly help to understand this. [murphy]