Double vs single quotes

RubyStringQuotesDouble QuotesSingle Quotes

Ruby Problem Overview


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"

Ruby Solutions


Solution 1 - Ruby

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

world_type = 'Mars'
"Hello #{world_type}"

Solution 2 - Ruby

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 

Solution 3 - Ruby

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.

Solution 4 - Ruby

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.

Solution 5 - Ruby

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')

Solution 6 - Ruby

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

Solution 7 - Ruby

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 :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionimjpView Question on Stackoverflow
Solution 1 - RubyJitsView Answer on Stackoverflow
Solution 2 - Rubylfx_coolView Answer on Stackoverflow
Solution 3 - RubyaloucasView Answer on Stackoverflow
Solution 4 - RubyAmokrane ChentirView Answer on Stackoverflow
Solution 5 - RubyBryan LiuView Answer on Stackoverflow
Solution 6 - RubyYarinView Answer on Stackoverflow
Solution 7 - RubyGeoView Answer on Stackoverflow