Platinum Partner

How To Display A String With Double Quotes

To escape quotes in a string %q (single quotes) and %Q (double quotes) are typically used e.g.

testing = "testing with \"quotes\""
puts testing      #=> testing with "quotes"
testing.inspect   #=> "\"testing with \\\"quotes\\\"\""

testing = %q(testing with "quotes")
puts testing      #=> testing with "quotes"

testing.inspect   #=> "\"testing with \\\"quotes\\\"\""

testing = %Q(testing with "quotes")
puts testing      #=> testing with "quotes"
testing.inspect   #=> "\"testing with \\\"quotes\\\"\""

testing = %q[testing with "quotes"]
puts testing      #=> testing with "quotes"
testing.inspect   #=> "\"testing with \\\"quotes\\\"\""

testing = %Q[testing with "quotes"]
puts testing      #=> testing with "quotes"
testing.inspect   #=> "\"testing with \\\"quotes\\\"\""

testing = %Q*testing with "quotes"*
puts testing      #=> testing with "quotes"
testing.inspect   #=> "\"testing with \\\"quotes\\\"\""


# -- with nested ruby code ---------

testing = "testing #{Time.now} with \"quotes\""
puts testing     #=> "testing 2010-04-03 10:25:14 +0100 with "quotes"
testing.inspect  #=> "\"testing 2010-04-03 10:25:14 +0100 with \\\"quotes\\\"\""

testing = %q(testing #{Time.now} with "quotes")
puts testing     #=> testing #{Time.now} with "quotes"
testing.inspect  #=> "\"testing \\\#{Time.now} with \\\"quotes\\\"\""

testing = %Q(testing #{Time.now} with "quotes")
puts testing     #=> "testing 2010-04-03 10:25:14 +0100 with "quotes"
testing.inspect  #=> "\"testing 2010-04-03 10:25:14 +0100 with \\\"quotes\\\"\""

testing = %q[testing #{Time.now} with "quotes"]
puts testing     #=> testing #{Time.now} with "quotes"
testing.inspect  #=> "\"testing \\\#{Time.now} with \\\"quotes\\\"\""

testing = %Q[testing #{Time.now} with "quotes"]
puts testing     #=> "testing 2010-04-03 10:25:14 +0100 with "quotes"
testing.inspect  #=> "\"testing 2010-04-03 10:25:14 +0100 with \\\"quotes\\\"\""

testing = %Q*testing #{Time.now} with "quotes"*
puts testing     #=> "testing 2010-04-03 10:25:14 +0100 with "quotes"
testing.inspect  #=> "\"testing 2010-04-03 10:25:14 +0100 with \\\"quotes\\\"\""


Resources:
 - Ruby Programming/Alternate quotes - Wikibooks, collection of open-content textbooks [wikibooks.org]
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}