String substitution

I don’t understand the following behaviour:

a = 'x'
puts a.gsub('x', 'a \& b') # => a x b
puts a.gsub('x', 'a \\& b') # => a x b

I’am expecting: a \& b

& has a special meaning - it’s a back-reference: Method: String#gsub — Documentation for core (3.0.2)

Try:

 > a.gsub('x', 'a \\\\& b')
 => "a \\& b"
1 Like

Thank you very much for your answer.