File tree Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
9
9
10
10
For a steady stream of TILs, [ sign up for my newsletter] ( https://tinyletter.com/jbranchaud ) .
11
11
12
- _ 865 TILs and counting..._
12
+ _ 866 TILs and counting..._
13
13
14
14
---
15
15
@@ -706,6 +706,7 @@ _865 TILs and counting..._
706
706
- [ Set RVM Default Ruby] ( ruby/set-rvm-default-ruby.md )
707
707
- [ Show Public Methods With Pry] ( ruby/show-public-methods-with-pry.md )
708
708
- [ Silence The Output Of A Ruby Statement In Pry] ( ruby/silence-the-output-of-a-ruby-statement-in-pry.md )
709
+ - [ Single And Double Quoted String Notation] ( ruby/single-and-double-quoted-string-notation.md )
709
710
- [ Squeeze Out The Extra Space] ( ruby/squeeze-out-the-extra-space.md )
710
711
- [ String Interpolation With Instance Variables] ( ruby/string-interpolation-with-instance-variables.md )
711
712
- [ Summing Collections] ( ruby/summing-collections.md )
Original file line number Diff line number Diff line change
1
+ # Single And Double Quoted String Notation
2
+
3
+ If you are building a string that involves interpolation and literal double
4
+ quotes, then you'll have to do some escaping. Here is an example:
5
+
6
+ ``` ruby
7
+ > feet, inches = [6 , 4 ]
8
+ > puts " I am #{ feet } '#{ inches } \" tall"
9
+ I am 6 ' 4" tall
10
+ ```
11
+
12
+ Having to escape a single instance of a double quote isn' t so bad. If you find
13
+ yourself having to do it a bunch, Ruby has something for you. It is a string
14
+ syntax feature called [Percent Notation ](percent- notation.md).
15
+
16
+ You can use percent notation to define double- quoted strings using ` Q` :
17
+
18
+ ` ` ` ruby
19
+ > puts %Q[I am #{ feet } '#{ inches } " tall]
20
+ I am 6'4" tall
21
+ ` ` `
22
+
23
+ No need to escape the double quote here.
24
+
25
+ There is a single- quoted version as well using ` q` :
26
+
27
+ ` ` ` ruby
28
+ > puts %q[I am #{ feet } '#{ inches } \" tall]
29
+ I am #{ feet } '#{ inches } \" tall
30
+ ` ` `
31
+
32
+ This is notably less useful than ` %Q` . For that reason, ` %Q` makes sense as a
33
+ default and it makes up the percent notations unmodified behavior:
34
+
35
+ ` ` ` ruby
36
+ > puts %[I am #{ feet } '#{ inches } " tall]
37
+ I am 6'4" tall
38
+ ` ` `
You can’t perform that action at this time.
0 commit comments