How can I escape single and double quotes in a string?
I want to escape single and double quotes together. I know how to pass them separately but don\'t know how to
One caveat:
Using %Q[]
and %q[]
for string comparisons is not intuitively safe.
For example, if you load something meant to signify something empty, like ""
or ''
, you need to use the actual escape sequences. For example, let's say qvar
equals ""
instead of any empty string.
This will evaluate to false
if qvar == "%Q[]"
As will this,
if qvar == %Q[]
While this will evaluate to true
if qvar == "\"\""
I ran into this issue when sending command-line vars from a different stack to my ruby script. Only Gabriel Augusto's answer worked for me.
You can use Q strings which allow you to use any delimiter you like:
str = %Q|ruby 'on rails" " = ruby 'on rails|
Use backslash to escape characters
str = "ruby \'on rails\" "