John Topley
John Topley

Reputation: 115372

Which style of Ruby string quoting do you favour?

Which style of Ruby string quoting do you favour? Up until now I've always used 'single quotes' unless the string contains certain escape sequences or interpolation, in which case I obviously have to use "double quotes".

However, is there really any reason not to just use double quoted strings everywhere?

Upvotes: 46

Views: 16722

Answers (9)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85575

Single quote preserve the characters inside them. But double quotes evaluate and parse them. See the following example:

"Welcome #{@user.name} to App!" 

Results:

Welcome Bhojendra to App!

But,

'Welcome #{@user.name} to App!'

Results:

Welcome #{@user.name} to App!

Upvotes: 1

Dejan Simic
Dejan Simic

Reputation: 8120

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

Upvotes: 47

Myrddin Emrys
Myrddin Emrys

Reputation: 43985

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.

Upvotes: 28

justingordon
justingordon

Reputation: 12913

I see arguments for both:

For using mostly double quotes:

enter image description here

  • It's easier to search for a string foobar by searching for "foobar" if you were consistent with quoting. However, I'm not. So I search for ['"]foobar['"] turning on regexps.

For using some combination of single double quotes:

  • Know if you need to look for string interpolation.
  • Might be slightly faster (although so slight it wasn't enough to affect the github style guide).

Upvotes: 6

Pistos
Pistos

Reputation: 23792

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.

Upvotes: 11

Gavin Kistner
Gavin Kistner

Reputation:

I used to use single quotes until I knew I needed interpolation. Then I found that I was wasting a lot of time when I'd go back and have to change some single-quotes to double-quotes. Performance testing showed no measurable speed impact of using double-quotes, so I advocate always using double-quotes.

The only exception is when using sub/gsub with back-references in the replacement string. Then you should use single quotes, since it's simpler.

mystring.gsub( /(fo+)bar/, '\1baz' )
mystring.gsub( /(fo+)bar/, "\\1baz" )

Upvotes: 5

Can Berk G&#252;der
Can Berk G&#252;der

Reputation: 113340

I use single quotes unless I need interpolation, or the string contains single quotes.

However, I just learned the arbitrary delimiter trick from Dejan's answer, and I think it's great. =)

Upvotes: 1

Orion Edwards
Orion Edwards

Reputation: 123652

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.

Upvotes: 31

Todd Gamblin
Todd Gamblin

Reputation: 59837

I usually use double quotes unless I specifically need to disable escaping/interpolation.

Upvotes: 8

Related Questions