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
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"
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.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs