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 (2.7.0)


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

Thank you very much for your answer.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs