0
            txt = "I'm happy :)"     #This is input from user
            txt = txt.gsub("'","\\'")
            raise :test

I'm getting "Im happy :)m happy :)"

I want to get this string variable's value is "I\'m happy :)" (with one backslash)

I'm using Rails. I will use this string variable to parameter for this situation I cannot use "puts" method. I tried many methods(%W(...), %(...)) to solve but I couldn't it yet.

Hasan
  • 11
  • 3

1 Answers1

1

\' is interpreted as the string to the right of the match, so it is doing what it should (see regex match context).

to do what you want you can do

gsub("'","\\\\'"}

or

gsub("'") { |s| "\\'" }
ishando
  • 306
  • 3
  • 7