219

Is there a specific time when I should use "" vs ''?

I've been using single quotes most of the time because it's easier to type but I'm not sure if I should.

e.g. get 'user/new' vs. get "user/new"

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
imjp
  • 6,495
  • 10
  • 48
  • 58

7 Answers7

235

" " allows you to do string interpolation, e.g.:

world_type = 'Mars'
"Hello #{world_type}"
Jits
  • 9,647
  • 1
  • 34
  • 27
  • 2
    So basically, just use double quotes all the time, right? – nekonari Jun 14 '18 at 22:42
  • 12
    No - double quotes simply allow for string interpolation. If you do not want string interpolation, then you should not use double quotes. – Panh Jul 11 '18 at 13:58
  • 18
    There is no reason to use double quotes only when you need interpolation, there is no significant performance impact and it will just make you think what you have to use. Just use double-quoted strings. – sidney Jun 10 '19 at 11:07
  • @sidney I like to think about it as: "Use single quotes everywhere, you'll know when you have to use the double form". – Jeremie Ges Dec 25 '22 at 23:25
158

except the interpolation, another difference is that 'escape sequence' does not work in single quote

puts 'a\nb' # just print a\nb 
puts "a\nb" # print a, then b at newline 
lfx_cool
  • 5,252
  • 2
  • 24
  • 25
  • 46
    Except for escaping single quotes themselves, e.g `'don\'t'`. – Sparhawk May 23 '13 at 01:10
  • 4
    This is a very useful answer to this qvestion. I never realized that single-quoting was for printing literally everything between the single quotes, with the exception of quotes which must be escaped. Thank you. – Jay Godse Jul 12 '13 at 14:23
  • 7
    And backslash also try `puts '\\'` It will only print single slash. see here http://stackoverflow.com/questions/25499046/is-it-possible-to-escape-character-within-single-quotes-in-ruby/25499079 – Hardik Aug 26 '14 at 06:31
  • 2
    yup, does are the only two cases you can escape with single quotes – Alexis Oct 29 '16 at 15:13
  • @Alexis not sure, maybe a little difference on the performance – lfx_cool Nov 02 '16 at 03:30
  • i was pointing that there aren't more cases for escaping characters(`\'` and `\\`), not that there aren't more differences between single and double quotes, in any case the performance difference is truly insignificant http://stackoverflow.com/a/1837604/1802527 – Alexis Nov 02 '16 at 12:44
  • Got burned when trying to gsub all newlines, never do this: str.gsub('\n', '
    ') , do this: str.gsub("\n", '
    ')
    – Joel Blum Aug 30 '18 at 11:31
55

There is a difference between single '' and double quotes "" in Ruby in terms of what gets to be evaluated to a string.

Initially, I would like to clarify that in the literal form of a string whatever is between single or double quotes gets evaluated as a string object, which is an instance of the Ruby String class.

Therefore, 'stackoverflow' and "stackoverflow" both will evaluate instances of String class with no difference at all.

The difference

The essential difference between the two literal forms of strings (single or double quotes) is that double quotes allow for escape sequences while single quotes do not!

A string literal created by single quotes does not support string interpollation and does not escape sequences.

A neat example is:

"\n" # will be interpreted as a new line

whereas

'\n' # will display the actual escape sequence to the user

Interpolating with single quotes does not work at all:

'#{Time.now}'
=> "\#{Time.now}" # which is not what you want..

Best practice

As most of the Ruby Linters suggest use single quote literals for your strings and go for the double ones in the case of interpolation/escaping sequences.

aloucas
  • 2,967
  • 1
  • 19
  • 15
  • 1
    So if you had a = '\n' and b = "#{a}", would b be interpreted as a new line or as the escape sequence? – NathanTempelman Dec 09 '16 at 20:17
  • 5
    The moment you assign a = '\n' a gets interpreted as "\\n" by interpollating a to b you wont get a new line. b will evalutate to "\\n" (no new line you get the escape sequence). a = '\n' => "\\n" where as a = "\n" => "\n" – aloucas Dec 10 '16 at 21:39
  • There is no reason to use double quotes only when you need interpolation, there is no significant performance impact, and it will just make you think about what you have to use. Just use double-quoted strings. Although you may call it best practice, it's nonsense. – sidney Dec 26 '22 at 11:22
  • @aloucas Thanks for this explanation! I wish there was somewhere this unexpected behaviour is described in the Ruby documentation. I ask this because I've spent the whole day debugging a "hashing" issue that was caused by using single rather than double quotes. – AndyPandy_25Flow Aug 11 '23 at 12:04
43

To answer your question, you have to use "" when you want to do string interpolation:

a = 2
puts "#{a}"

Use simple quotes otherwise.

Also if you are wondering about whether there is a difference in terms of performance, there is an excellent question about this on StackOverflow.

And if you are really new to RoR, I urge you to pick up a decent Ruby book to learn the basics of the language. It will help you understand what you are doing (and will keep you from thinking that Rails is magic). I personally recommend The Well grounded Rubyist.

Community
  • 1
  • 1
Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
6

Similar to the answer "\n" in printing, following is another case of the difference

puts "\1"  -> get special character
puts '\1'  -> get \1

so looks like * was convert to escaped character in double quotes, but not in single quotes. BTW, it will impact the output when using in regular expression e.g., str.gsub(/regular expression/, '\1,\2')

Bryan Liu
  • 81
  • 1
  • 4
5

Another reason you would want to use single-quotes is when passing a regex pattern as a string:

This regex pattern will work because passed within single-quotes:

"123 ABC".match('\d')
=> #<MatchData "1">

This regex pattern will fail because passed within double-quotes (you would have to double-escape it to get it to work):

"123 ABC".match("\d")
=> nil
Yarin
  • 173,523
  • 149
  • 402
  • 512
-9

In this specific case, it makes no difference how you write it. They are equivalent. Also, you may want to read some more Ruby guides/tutorials :)

Geo
  • 93,257
  • 117
  • 344
  • 520