2

In ruby using a single quote is faster than double quotes as we can see in the message : Is there a performance gain in using single quotes vs double quotes in ruby?.

So why does everyone use double quotes (or both)???

Some examples :

(I know, it's not everyone but the majority.)

EDIT 1 : Interpolation is not a reason!

Double quotes are not always with interpolation or anything special like this, in Sinatra :

it "defines HEAD request handlers with HEAD" do
Community
  • 1
  • 1
Dougui
  • 7,142
  • 7
  • 52
  • 87
  • I answered below, but this is also a dupe question, see http://stackoverflow.com/questions/1836467/is-there-a-performance-gain-in-using-single-quotes-vs-double-quotes-in-ruby for more info – Khallil Mangalji Aug 01 '12 at 13:55

5 Answers5

7

Double quotes allows you to do interpolation : "Number of users : #{@count_user}"

Plus taking a look at the benchmarks, I'd say that at best it doesn't matter, the overhead is very small, and some benchmarks are actually faster with double quotes ...

Anthony Alberto
  • 10,325
  • 3
  • 34
  • 38
5

Double quotes allow for string interpolation, single quotes will not evaluate anything within them while the double quotes will. For example, the double quotes will allow you to have escape characters and single quotes won't!

  "he said \n hello"

will result in 2 lines while

  'he said \n hello'

will result in a string with '\n' in the middle of it

Matteo Alessani
  • 10,264
  • 4
  • 40
  • 57
Khallil Mangalji
  • 471
  • 5
  • 13
2

One reason is that string interpolation is supported in double quotes but not single quotes.

For example:

some_var = 15
"I have #{some_var} cats"
# => "I have 15 cats"

'I have #{some_var} cats"
# => "I have \#{some_var} cats"

I personally prefer single quotes when possible because I think they look cleaner. But string interpolation is one very important reason to use double quotes.

With respect to the performance issue, it's so negligible that it's really a matter of preference rather than performance.

BaronVonBraun
  • 4,285
  • 23
  • 23
0

I don't know if you actually read that thread properly but @zetetic actually found that using double quotes is slightly faster. Nonetheless this test was done over 1 million assignments and thus in that regard they'r both basically the same speed.

It all comes down to preference. A lot of people come from languages where single quotation are just not possible and so they are used to double quotations. Its all a matter of preference.

Florin Stingaciu
  • 8,085
  • 2
  • 24
  • 45
0

It's been answered and accepted, and some of these are repeating others, but my 2¢ worth.

  1. Using double quotes means that if you ever do want to add something that needs interpolation into a string later, you can just do so without having to change the quotes.
  2. Consistency between strings that do require interpolation and those that don't.
  3. Being able to include all English in a string, eg. "don't", "Jim's" without having to escape it, or change the quotes.
  4. Embedding \ escape chars in strings like \n and \t
smathy
  • 26,283
  • 5
  • 48
  • 68