Search code examples
rubyrubymine

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


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?


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?