Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any advantage to _ever_ using a single quote around a string in Ruby/Rails?

I understand the functional difference between single and double quotes in Ruby, but I'm wondering what concrete reasons people have for varying between the two. In my mind it seems like you should just always use a double quote, and not think about it.

A couple rationales that I've read in researching the topic...

  1. Use a single quote unless a double quote is required.

  2. There's a very, very minor performance advantage to a single quotes.

Any other interesting thoughts out there? (Or maybe this is a case of the freedom or Ruby leaving the door open for no One Right Way to do something...)

like image 528
Bob. Avatar asked Mar 31 '12 21:03

Bob.


People also ask

Can you use single quotes for string?

Single-quoted Strings: It is the easiest way to define a string. You can use it when you want the string to be exactly as it is written. All the escape sequences like \r or \n, will be output as specified instead of having any special meaning. Single-quote is usually faster in some cases.

Can you use single quotes in Ruby?

If you've written Ruby, you've heard it before: Use single quoted strings unless you need string interpolation.

What is the main difference between single quotes and double quotes Ruby?

The basic difference between these two methods is Single quotes can not hold/print escape sequences directly, while double quotes can. i.e. Double quoted strings are used for String Interpolation in Ruby.


3 Answers

I usually follow the following rule:

never use double quotes (or %Q or %W) if you don't interpolate

The reason for this is that if you're trying to track down an error or a security bug, you immediately know when looking at the beginning of the string that there cannot possibly any code inside it, therefore the bug cannot be in there.

However, I also follow the following exception to the rule:

use double quotes if they make the code more readable

I.e. I prefer

"It's time"

over

'It\'s time'
%q{It's time}

It is technically true that single quoted strings are infinitesimally faster to parse than double quoted strings, but that's irrelevant because

  • the program only gets parsed once, during startup, there is no difference in runtime performance
  • the performance difference really is extremely small
  • the time taken to parse strings is irrelevant compared to the time taken to parse some of Ruby's crazier syntax

So, the answer to your question is: Yes, there is an advantage, namely that you can spot right away whether or not a string may contain code.

like image 150
Jörg W Mittag Avatar answered Sep 23 '22 02:09

Jörg W Mittag


I can think of three reasons to use single quoted strings:

  1. They look cleaner (the reason I use them)
  2. They make it easier to create a string you'd otherwise have to escape ('he said "yes"' vs "he said \"yes\"")
  3. They are slightly more performant.
like image 24
Joshua Cheek Avatar answered Sep 24 '22 02:09

Joshua Cheek


I would assume using a single-quoted string is faster, since double quotes allow string interpolation, and single-quoted strings do not.

That's the only difference I know of. For that reason, it's probably best to only use a single-quoted string unless you need string interpolation:

num = 59
"I ate #{num} pineapples"`
like image 23
Edd Morgan Avatar answered Sep 25 '22 02:09

Edd Morgan