Escaping single and double quotes in a string in ruby?

RubyEscaping

Ruby Problem Overview


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 pass both of them.

e.g: str = "ruby 'on rails" " = ruby 'on rails"

Ruby Solutions


Solution 1 - Ruby

My preferred way is to not worry about escaping and instead use %q, which behaves like a single-quote string (no interpolation or character escaping), or %Q for double quoted string behavior:

str = %q[ruby 'on rails" ] # like single-quoting
str2 = %Q[quoting with #{str}] # like double-quoting: will insert variable

See https://docs.ruby-lang.org/en/trunk/syntax/literals_rdoc.html#label-Strings and search for % strings.

Solution 2 - Ruby

Use backslash to escape characters

str = "ruby \'on rails\" "

Solution 3 - Ruby

Here is a complete list:

enter image description here

From http://learnrubythehardway.org/book/ex10.html

Solution 4 - Ruby

You can use Q strings which allow you to use any delimiter you like:

str = %Q|ruby 'on rails" " = ruby 'on rails|

Solution 5 - Ruby

>> str = "ruby 'on rails\" \" = ruby 'on rails"
=> "ruby 'on rails" " = ruby 'on rails"

Solution 6 - Ruby

I would go with a heredoc if I'm starting to have to worry about escaping. It will take care of it for you:

string = <<MARKER 
I don't have to "worry" about escaping!!'"!!
MARKER

MARKER delineates the start/end of the string. start string on the next line after opening the heredoc, then end the string by using the delineator again on it's own line.

This does all the escaping needed and converts to a double quoted string:

string
=> "I don't have to \"worry\" about escaping!!'\"!!\n"

Solution 7 - Ruby

I would use just:

str = %(ruby 'on rails ")

Because just % stands for double quotes(or %Q) and allows interpolation of variables on the string.

Solution 8 - Ruby

Here is an example of how to use %Q[] in a more complex scenario:

  %Q[
    <meta property="og:title" content="#{@title}" />
    <meta property="og:description" content="#{@fullname}'s profile. #{@fullname}'s location, ranking, outcomes, and more." />
  ].html_safe

Solution 9 - Ruby

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAleemView Question on Stackoverflow
Solution 1 - RubyRob Di MarcoView Answer on Stackoverflow
Solution 2 - Rubygaurav.singharoyView Answer on Stackoverflow
Solution 3 - RubyGabriel AugustoView Answer on Stackoverflow
Solution 4 - RubyJonathanView Answer on Stackoverflow
Solution 5 - RubyJohn La RooyView Answer on Stackoverflow
Solution 6 - RubyjbarrView Answer on Stackoverflow
Solution 7 - RubyAlexandro de OliveiraView Answer on Stackoverflow
Solution 8 - RubyAbramView Answer on Stackoverflow
Solution 9 - RubykayleeFrye_onDeckView Answer on Stackoverflow