I simply don't understand if this behavior is expected or not.
I am trying to replace the '
character in a string by the 2 characters \'
(basically escaping the quote by adding a \
before it)
My naive approach was this str.gsub("'", "\\'")
but I got weirds results.
I finally got it to work by using str.gsub("'") { "\\'" }
but I would like to understand the behavior of gsub here.
Here is what I tried :
str2 = "super'test"
=> "super'test"
str2.gsub("'", "\\'")
=> "supertesttest" # <==== ??? Where does that comes from ?
# Further tests are to understand if `gsub` behaves as I would expect, and it does...
str2.gsub("'", "")
=> "supertest"
str2.gsub("'", "\\")
=> "super\\test"
str2.gsub("t", "ab")
=> "super'abesab"
str2.gsub("t", "\b")
=> "super'\bes\b"
str2.gsub("t", "\\b")
=> "super'\\bes\\b"
str2.gsub("t", "\'")
=> "super''es'"
str2.gsub("'", "\'")
=> "super'test"
str2.gsub("'", "\\ '")
=> "super\\ 'test"
str2.gsub("'", "\\'")
=> "supertesttest" # I really don't get it....
str2.gsub("'") { "\\'" }
=> "super\\'test" # This works