-1

I noticed something odd when I was working with string interpolation and I arbitrarily decided to use single quotes for my string that had interpolation in it. However, single quotes seem to act weird with string interpolation. For example:

bob = 'bob'

puts 'hello #{bob}'
# The above prints out:
# hello #{bob}

Strangely, though, when I decided to use double quotes instead, the result of the string interpolation was different:

bob = 'bob'

puts "hello #{bob}"
# The above prints out:
# hello bob

So my question is: are double quotes supposed to be different from single quotes in regard to string interpolation, and if so, is there any particular reason why?

GDP2
  • 1,948
  • 2
  • 22
  • 38
  • 4
    This question has been asked many times on SO. [Here](http://stackoverflow.com/questions/20870375/single-vs-double-quotes) is one duplicate, that is itself a duplicate. Please do basic research before posting a question. – Cary Swoveland Mar 10 '15 at 22:30
  • 1
    @CarySwoveland I have done research on this; no question or resource on this turned up on my first few pages of Google. Also, this question is specifically about string interpolation with singe vs. double quotes, not their differences in general. – GDP2 Mar 10 '15 at 22:32
  • String interpolation is the main difference: you can't do it with single quotes. "Why" is a big question, but you can ask it. Note the title of your question. – Cary Swoveland Mar 10 '15 at 22:33
  • @CarySwoveland: *Why* is almost certainly because that's how Perl does it, Perl does it because `/bin/sh` does it, `/bin/sh` does it because it needs a reasonable way to deal with whitespace inside values without having to worry about stray dollar signs. – mu is too short Mar 12 '15 at 16:40

2 Answers2

1

The diference is that single quotes doesn't have string interpolation.

The recommendation is to single quotes for strings that don't need string interpolation, this doesn't really give an speed advantage, is more of a style indication, ie, you know that a string with single quotes is a literal string.

tiagotex
  • 56
  • 5
1

Yes, the answer is that you cannot put variables in single quoted strings, eg string interpolation is not allowed in single quoted strings. Further, you might be interested, escape sequences do not work in single quoted strings (Except for escaping single quotes themselves like 'don\'t').

There is also some debate about whether or not there's performance benefits of single vs double quotes, but I'm not seeing any convincing cases.

Community
  • 1
  • 1
ylluminate
  • 12,102
  • 17
  • 78
  • 152
  • What you say is correct, but "cannot put variables in single quoted strings" could be interpreted as "single quoted strings can only contain characters", which would be incorrect: `"ab".gsub(/(.)/,'\1\1') #=> "aabb"`. – Cary Swoveland Mar 10 '15 at 23:08
  • There might be debate, but it's going to be from people who forget that the interpreter processes the strings before the script itself starts running. If two duplicate strings exist, one delimited by single-quotes and the other by double quotes, there will be no difference in runtime speed. Only variable interpolation into the string will have an effect. – the Tin Man Mar 11 '15 at 00:00