0
Is there a difference between single and double quoted strings in Ruby?
For example, "Zen" versus 'Zen'
2 Antworten
+ 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.)
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.