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]