Why does Rubymine suggests I use single-quotes over double-quotes on strings?

StackOverflow https://stackoverflow.com/questions/22469804

  •  16-06-2023
  •  | 
  •  

Question

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?

Était-ce utile?

La solution

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?

Autres conseils

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.

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top