6

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...)

Bob.
  • 1,737
  • 1
  • 15
  • 20
  • I think there's a question examining if single quotes are faster. – Andrew Grimm Mar 31 '12 at 21:28
  • 1
    [here](http://stackoverflow.com/questions/6395288/ruby-double-vs-single-quotes) among a lot of other questions. String interpolation is the big reason. Jorg had a good reply here though. – Jonathan Apr 01 '12 at 00:26

7 Answers7

10

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.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
4

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.
Joshua Cheek
  • 30,436
  • 16
  • 74
  • 83
1

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"`
Edd Morgan
  • 2,873
  • 17
  • 22
1

Well, there are a lot of fuzz about the "performance gain" of single quoted strings vs double quoted strings.

The fact is that it doesn't really matter if you don't interpolate. There are a lot of benchmarks around the web that corroborate that assertion. (Some here at stackoverflow)

Personally, I use double for strings that have interpolation just for the sake of readability. I prefer to see the double quotes when I need them. But in fact there are methods in ruby for interpolating strings other than "double quoting" them:

%q{#{this} doesn't get interpolated}

%Q{#{this} is interpolated}
Community
  • 1
  • 1
Castilho
  • 3,147
  • 16
  • 15
0
1.9.2-p290 :004 > x = 3
 => 3 
1.9.2-p290 :005 > "#{x}"
 => "3" 
1.9.2-p290 :006 > '#{x}'
 => "\#{x}"

In any other case, i prefer single quotes, because it's easier to type and just makes the code less overbloated to my eyes.

Spyros
  • 46,820
  • 25
  • 86
  • 129
  • 1
    A single sentence along the lines of "single-quoted strings don't interpolate" or something would make this answer. The code's a great illustration of the fact, but some people might not understand what is (or isn't) going on here. (Or maybe they do now, if they read comments. :)) – cHao Mar 31 '12 at 21:21
  • 3
    the OP knew the functional difference between the two, he stated that clearly – Castilho Mar 31 '12 at 21:27
  • Well ok, both answers so far talk about the same thing though :) – Spyros Mar 31 '12 at 21:47
  • 2
    @Castilho: The entire difference between the two is a functional difference. To say "i understand the functional difference", and still ask the question, is to *not* understand the functional difference. :P – cHao Apr 02 '12 at 13:44
-1

Since asking this question I've discovered this unofficial Ruby Style Guide that addresses this, and many many more styling questions I've had floating around in my head. I'd highly recommend checking it out.

Bob.
  • 1,737
  • 1
  • 15
  • 20
-1

I found that when putting variables in a string using #{} did not work in single quotes, but did work in double quotes as below.

comp_filnam and num (integer) are the variables I used to create the file name in the file path:

file_path_1 = "C:/CompanyData/Components/#{comp_filnam}#{num.to_s}.skp"
logi-kal
  • 7,107
  • 6
  • 31
  • 43