Is there a difference between single and double quoted strings in Ruby? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there a difference between single and double quoted strings in Ruby?

For example, "Zen" versus 'Zen'

11th Dec 2019, 6:43 PM
Litong Deng
Litong Deng - avatar
2 Answers
+ 5
Litong Deng In general there is no any difference when you use string in single quote and double quotes when you are use the example written by you "Zen" and 'Zen' But in some cases Ruby interpolates values of expressions in #{variable} only in "double-quoted" strings: puts "answer = #{1+2}" #=> answer = 3 puts 'answer = #{1+2}' #=> answer = #{1+2} Also escape sequences such as \n for newline and \t for a tab only work in double quotes not in single quotes in general In any situation if you have to use both single and double quotes then you can use %Q %Q("That's how", you do that.)
11th Dec 2019, 6:53 PM
GAWEN STEASY
GAWEN STEASY - avatar
0
There is also a small difference in performance. The rule is simple, do not use double quotes where you don’t need to interpolate.
13th Dec 2019, 1:51 PM
Alex Tatarnikov