14

Every time I use double-quoted strings, I'm getting this kind of suggestion:

enter image description here

When I click the bulb icon I'm getting an option to convert that string into a single-quoted string.

Can someone explain why single-quoted strings are preferred over double-quoted strings?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
PrivateUser
  • 4,474
  • 12
  • 61
  • 94

3 Answers3

22

Single quotes are preferred if there is no interpolation in the string. Ruby will work less (in theory) to output single quote strings which in turn will speed up your code some (again in theory). That is one reason why RubyMine suggests it.

Another reason is for plain readability. Which you can read about here in the style guide: Ruby Coding Style Guide

Benchmark tests has proven that speed gains from using single over double quoted strings is negligible compared to the actual execution time.

Ultimately the answer comes down to style. To learn about performance check out this question: Is there a performance gain in using single quotes vs double quotes in ruby?

Community
  • 1
  • 1
MrPizzaFace
  • 7,807
  • 15
  • 79
  • 123
  • Ahh. I see... Now that makes sense. – PrivateUser Mar 18 '14 at 03:32
  • 1
    It can also help you catch errors. For example, say you wrote `puts "Age: ${age}"`, RubyMine will warn you that there is no interpolation, and you would then (hopefully) notice that you accidentally used the wrong symbol (`$` instead of `#`). – Cam Jackson Jun 12 '14 at 03:03
  • 14
    If you would like to **disable this warning** you can do so under: `Preferences` > `Inspections` > `Ruby` > `Double quoted string` – originalhat Aug 08 '14 at 19:24
  • 4
    Slight correction to @Devin_Brown's post. The latest version of RubyMine has the preference here: Preferences > Editor > Inspections > Ruby > Double quoted string – Tod Birdsall Nov 24 '14 at 16:15
  • This inspection is ON by default and I believe it must be OFF by default. This is so annoying to find it active in every new Ruby project I open. – Alex Kovshovik Jul 07 '15 at 02:24
5

There used to be a performance difference, so rubymine(and most of the community) recommended single quotes when you were not interpolating.

The perf difference no longer exists. http://viget.com/extend/just-use-double-quoted-ruby-strings

You can disable the warnings:

Preferences > Editor > Inspections > Ruby > Double quoted string

Unfortunately you cannot reverse the warnings and show warnings for single-quoted strings.

Blair Anderson
  • 19,463
  • 8
  • 77
  • 114
1

Single quotes will preserve escape characters such as as \n whereas these characters will escape double quotes.

I don't currently have RubyMine installed as my trial ran out, but I'm willing to bet you can change this in preferences if you would prefer to use double quotes and the suggestions bother you.

erahm
  • 344
  • 1
  • 5