Aleem
Aleem

Reputation: 851

Escaping single and double quotes in a string in ruby?

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"

Upvotes: 85

Views: 119066

Answers (9)

Rob Di Marco
Rob Di Marco

Reputation: 44942

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.

Upvotes: 104

kayleeFrye_onDeck
kayleeFrye_onDeck

Reputation: 6958

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.

Upvotes: 2

Alexandro de Oliveira
Alexandro de Oliveira

Reputation: 1359

I would use just: str = %(ruby 'on rails ") Because just % stands for double quotes(or %Q) and allows interpolation of variables on the string.

Upvotes: 7

Gabriel Augusto
Gabriel Augusto

Reputation: 1012

Here is a complete list:

enter image description here

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

Upvotes: 31

jbarrieault
jbarrieault

Reputation: 464

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"

Upvotes: 7

Abram
Abram

Reputation: 41874

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

Upvotes: 1

gaurav.singharoy
gaurav.singharoy

Reputation: 3921

Use backslash to escape characters

str = "ruby \'on rails\" "

Upvotes: 38

Jonathan
Jonathan

Reputation: 3253

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

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

Upvotes: 23

John La Rooy
John La Rooy

Reputation: 304137

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

Upvotes: 7

Related Questions