Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backslashes in single quoted strings vs. double quoted strings

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?

like image 690
Tucker Avatar asked Mar 15 '09 17:03

Tucker


People also ask

What is the difference in strings written in single and double quotes?

The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences. Each variable will be replaced by its value.

What is the difference between single quote (') and double quote )?

Single Quotes. The use of single quotes [ ' ... ' ] or double quotes [ “...” ] differs with context and geographic location. Conventionally, most English speaking countries use double quotes to mark direct speech and single quotes to mark speech within speech.

Should you use single or double quotation marks?

As a general rule, British usage has in the past usually preferred single quotes for ordinary use, but double quotes are now increasingly common; American usage has always preferred double quotes.

What do you meant by interpolating variables in double quoted strings?

When you define a string literal using double quotes or a heredoc, the string is subject to variable interpolation. Interpolation is the process of replacing variable names in the string with the values of those variables.


2 Answers

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 
like image 104
John Topley Avatar answered Sep 29 '22 14:09

John Topley


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.

like image 43
Linuxios Avatar answered Sep 29 '22 14:09

Linuxios