Ruby newbie here, and I understand that single-quote strings will not allow interpolation and will ignore most escape sequences. I'm just looking for some actual examples of when you might want to use single-quote strings as opposed to double-quote strings.
Asked
Active
Viewed 43 times
2 Answers
3
Single quotes are used when you don't need interpolation:
name = 'joe'
Double is used when you do need interpolation:
name = 'joe'
puts "The name is #{name}"
You can use double quotes in both cases. Many people find it simpler to use double quotes all the time.
You are correct about escape sequences.
puts "\nThis is a new line."
=>
=> This is a new line.
puts '\nThis is a new line.'
=> '\nThis is a new line.'
I prefer to use single quotes whenever possible, because double quotes are noisy.

B Seven
- 44,484
- 66
- 240
- 385
3
In addition to the difference in interpolation, it is also nice to be able to include quotes in the string literal without clumsy escapes. Just choose the opposite surrounding quotes:
name = 'Phil "the man" Smith'
name = "Jack O'Conner"

Thilo
- 257,207
- 101
- 511
- 656
-
1There's always the `%q{...}` and `%Q{...}` forms if you need to deal with lots of internal quotes. – mu is too short May 13 '15 at 01:29