views:

286

answers:

2

I don't understand what is going on here. How should I feed gsub to get the string "Yaho\'o"?

>> "Yaho'o".gsub("Y", "\\Y")
=> "\\Yaho'o"
>> "Yaho'o".gsub("'", "\\'")
=> "Yahooo"
+1  A: 
"Yaho'o".gsub("'", "\\\\'")

Because you're escaping the escape character as well as escaping the single quote.

mopoke
+3  A: 

\' means $' which is everything after the match. Escape the \ again and it works

"Yaho'o".gsub("'", "\\\\'")
ChaosR