31

If I add a backslash+space to the start of double and single quoted strings, I get different results:

"\ text"
'\ text' 

In the output for the double quoted string I see only a space.
In the output for the single quoted string I see backslash+space.

What's happening there? Is this because '\ ' is interpreted as a special character in the double quote string but in the single quoted string the characters are preserved as is?

If I change the strings to this, I see the same output, namely a single slash followed by a space and then the text:

"\\ text"
'\\ text' 

In both cases the backslash is escaped. I'm confused why they work the same way in this situation.

Is there some rule that would help to explain the fundamental difference between how single quoted strings and double quoted strings handle backslashes in Ruby?

sawa
  • 165,429
  • 45
  • 277
  • 381
Tucker
  • 323
  • 1
  • 3
  • 5
  • After your edits, your question is now essentially "is Wikibooks currently up-to-date for Ruby 1.9". Please edit the question title to match this. – bzlm Mar 15 '09 at 18:03

6 Answers6

24

Double-quoted strings support the full range of escape sequences, as shown below:

  • \a Bell/alert (0x07)
  • \b Backspace (0x08)
  • \e Escape (0x1b)
  • \f Formford (0x0c)
  • \n Newline (0x0a)
  • \r Return (0x0d)
  • \s Space (0x20)
  • \t Tab (0x09)
  • \v Vertical tab (0x0b)

For single-quoted strings, two consecutive backslashes are replaced by a single backslash, and a backslash followed by a single quote becomes a single quote:

'escape using "\\"' -> escape using "\"
'That\'s right'     -> That's right
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
John Topley
  • 113,588
  • 46
  • 195
  • 237
  • 1
    Is there any use for a single backslash by itself in double or single quoted strings? I'm just confused since it doesn't seem to do anything at all but disappear – thesowismine Jun 14 '15 at 01:38
10

Ruby only interprets escape sequences in double quoted strings. In a single quoted string, only \\ (backslash backslash) and \' (backslash quote) are taken as special characters. You should use double quoted strings only when you need more interpretation. Otherwise, single quotes provide a performance boost.

When you mentioned including the name of a variable, Ruby never does that. Just the variable name is treated as more of the string literal. To include the value of a variable (or any expression) put the expression in like this:

"#{variable}"

Note that this only works in double quoted strings. To add a variable to a single quoted one, you need to do this:

'The value of X is: '+X

If you need serious formatting, look into Ruby's version of sprintf and printf. They are pretty much wrappers around the C functions, and are quite powerful, but a bit cumbersome to use.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • Don't believe the part about the performance boost. It won't show up in benchmarks because Ruby parses the string as it loads the file and the difference disappears. We did a benchmark on SO somewhere in one of the questions, with results which debunked that idea. *If* there is a boost it is tiny and only will occur during the start-up of an app. – the Tin Man Feb 14 '13 at 13:17
  • @theTinMan: Do you think you can find me the benchmark? I'd like to look at it. Theoretically, there should be a performance boost from not having to look for the escape sequences, but that's just a tiny bit of optimized C that would be a hard difference to notice. – Linuxios Feb 14 '13 at 14:37
  • The [benchmark](http://stackoverflow.com/a/13039446/128421) in question was about various ways to define arrays. I think the same situation exists for strings, that the differences are removed as the script is loaded, and once in memory there is no real difference. It's easy to write a test to see for yourself based on the benchmark in that answer. – the Tin Man Feb 14 '13 at 15:34
  • @theTinMan: I don't think that's valid. The arrays are all defining the exact same thing. When you use " quotes vs. ' quotes, the interpreter has to actually do something additional and different. It's not just another syntax. – Linuxios Feb 14 '13 at 22:36
  • You might want to check out http://stackoverflow.com/questions/1836467/is-there-a-performance-gain-in-using-single-quotes-vs-double-quotes-in-ruby – the Tin Man Feb 14 '13 at 23:30
  • @theTinMan: Are we sure that those are different strings and that Ruby didn't intern the string and return the same one over and over again? – Linuxios Feb 14 '13 at 23:34
8

I'd refer you to "Ruby Programming/Strings" for a very concise yet comprehensive overview of the differences.

From the reference:

puts "Betty's pie shop"

puts 'Betty\'s pie shop'

Because "Betty's" contains an apostrophe, which is the same character as the single quote, in the second line we need to use a backslash to escape the apostrophe so that Ruby understands that the apostrophe is in the string literal instead of marking the end of the string literal. The backslash followed by the single quote is called an escape sequence.

Community
  • 1
  • 1
zvoase
  • 786
  • 6
  • 12
  • 6
    From ["How do I write a good answer?"](https://stackoverflow.com/help/how-to-answer): "Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline." – Richard Hansen Mar 28 '16 at 18:16
4

This is not a full answer (since the simple question has been answered already), but rather it is supplementary information.

Which style of Ruby string quoting do you favour?

Don't use double quotes if you have to escape them. And don't fall in "single vs double quotes" trap. Ruby has excellent support for arbitrary delimiters for string literals:

http://rors.org/2008/10/26/dont-escape-in-strings

I took that advice and have never looked back!

Community
  • 1
  • 1
Ian Terrell
  • 10,667
  • 11
  • 45
  • 66
3

Is this because the '\ ' is interpreted as a special character in the double quote string but in the single quoted string the characters are preserved as is?

Yes. Single-quoted strings are treated as literals; double-quoted strings are interpolated. This is the same in other Ruby-like languages, and hasn't changed in 1.9.

bzlm
  • 9,626
  • 6
  • 65
  • 92
0

you can make <%= f.label :nom_entreprise, "Nom de l'entreprise" %>

Liam
  • 27,717
  • 28
  • 128
  • 190