Which style of Ruby string quoting do you favour?

Don't use double quotes if you have to escape them. And don't fall in "single vs double quotes" trap. Ruby has excellent support for arbitrary delimiters for string literals:

Mirror of Site - https://web.archive.org/web/20160310224440/http://rors.org/2008/10/26/dont-escape-in-strings

Original Site - http://rors.org/2008/10/26/dont-escape-in-strings


I always use single quotes unless I need interpolation.

Why? It looks nicer. When you have a ton of stuff on the screen, lots of single quotes give you less "visual clutter" than lots of double quotes.

I'd like to note that this isn't something I deliberately decided to do, just something that I've 'evolved' over time in trying to achieve nicer looking code.

Occasionally I'll use %q or %Q if I need in-line quotes. I've only ever used heredocs maybe once or twice.


Like many programmers, I try to be as specific as is practical. This means that I try to make the compiler do as little work as possible by having my code as simple as possible. So for strings, I use the simplest method that suffices for my needs for that string.

<<END
For strings containing multiple newlines, 
particularly when the string is going to
be output to the screen (and thus formatting
matters), I use heredocs.
END

%q[Because I strongly dislike backslash quoting when unnecessary, I use %Q or %q
for strings containing ' or " characters (usually with square braces, because they
happen to be the easiest to type and least likely to appear in the text inside).]

"For strings needing interpretation, I use %s."%['double quotes']

'For the most common case, needing none of the above, I use single quotes.'

My first simple test of the quality of syntax highlighting provided by a program is to see how well it handles all methods of quoting.


I use single quotes unless I need interpolation. The argument about it being troublesome to change later when you need interpolation swings in the other direction, too: You have to change from double to single when you found that there was a # or a \ in your string that caused an escape you didn't intend.

The advantage of defaulting to single quotes is that, in a codebase which adopts this convention, the quote type acts as a visual cue as to whether to expect interpolated expressions or not. This is even more pronounced when your editor or IDE highlights the two string types differently.

I use %{.....} syntax for multi-line strings.