Single quotes vs. double quotes in Ruby

2015, Aug 23    

If you have a problem mentioned above, please read this article first. After that (and only then!) you’re allowed to continue reading this post ;-)

“Use single quoted strings unless you need string interpolation”. This sentence was one of the first things I’ve heard when I was starting my Ruby adventure. I followed this “rule” for a long time without even thinking about it. Now, as I’m digging deeper in Ruby, I wonder about things that didn’t really mean too much for me earlier. This is one of these things. And the result is that nowadays I simply use double-quoted strings. Everywhere.

First of all, you should think about benefits of using this or that. If you profit from one thing more than from another, you should consider disadvantages of both of them and then choose wisely. So let’s take a look of benefits coming from using single quoted strings instead of the double-quoted ones:

There is none.

Sooner or later you just have to use double-quoted strings in your project if you want to use Ruby’s string interpolation. If you use both of them, your files will be full of mixed single and double quotes. Some people say that in this way it’s easier to notice the piece of text mixed with variables - if you see double quotes then you know there should be interpolation somewhere. But how many times you’ll be looking for that? Consider situations where you need to change the single quotes in a dozen files to double quotes cause you’ve decided to change the code and you’ll be using string interpolation. Just ask yourself: is it worth thinking about “Should I use single quotes or double ones here, maybe I’ll use string interpolation later here”. It’s such a trivial problem that you should never think about that again and just start using double quotes everywhere.

Here are some facts why:

1. Do single quotes are parsed somehow different? Nope.

single quotes S-tree in irb:

2.1.5 :001 > require 'ripper'
 => true
2.1.5 :002 > puts Ripper.sexp(%q{'foo'}).inspect
[:program, [[:string_literal, [:string_content, [:@tstring_content, "foo", [1, 1]]]]]]
 => nil

Double quotes S-tree in irb:

2.1.5 :001 > require 'ripper'
 => true
2.1.5 :002 > puts Ripper.sexp(%q{"foo"}).inspect
[:program, [[:string_literal, [:string_content, [:@tstring_content, "foo", [1, 1]]]]]]
 => nil

2. Does the bytecode differ for these two? Nope.

single quotes bytecode:

2.1.5 :004 > puts RubyVM::InstructionSequence.disasm -> { 'foo' }
== disasm: <RubyVM::InstructionSequence:block in irb_binding@(irb)>=====
== catch table
| catch type: redo   st: 0002 ed: 0006 sp: 0000 cont: 0002
| catch type: next   st: 0002 ed: 0006 sp: 0000 cont: 0006
|------------------------------------------------------------------------
0000 trace            256                                             (   4)
0002 trace            1
0004 putstring        "foo"
0006 trace            512
0008 leave            
 => nil

double quotes bytecode:

2.1.5 :004 > puts RubyVM::InstructionSequence.disasm -> { "foo" }
== disasm: <RubyVM::InstructionSequence:block in irb_binding@(irb)>=====
== catch table
| catch type: redo   st: 0002 ed: 0006 sp: 0000 cont: 0002
| catch type: next   st: 0002 ed: 0006 sp: 0000 cont: 0006
|------------------------------------------------------------------------
0000 trace            256                                             (   4)
0002 trace            1
0004 putstring        "foo"
0006 trace            512
0008 leave            
 => nil

3. Is one of them somehow faster than the other? Not really.

There is no significant difference between them. If you do not believe, check the results in this blog post.

4. Does a single quoted string maybe looks better than a double-quoted one? Not for me.

That’s it. That’s why I started using only double-quoted strings in my Ruby projects. With some little exceptions ;-)