SundayMonday
SundayMonday

Reputation: 19727

In Ruby do single quotes need to be escaped in double quoted strings?

Say I have the following double-quoted string in Ruby:

"Person's name"

Does the single quote need to be escaped?

Upvotes: 1

Views: 137

Answers (3)

knut
knut

Reputation: 27845

Your test and the aother answers showed you already, that there is no difference.

p "Person's name"
p "Person\'s name"

With single quotes, it is a difference:

You get a syntax error with

p 'Person's name' 

The quoted version will work:

p 'Person\'s name'

If this behaviour irritated you, perhaps you prefer another possibility to create a String:

p %{Person's name}
p %q{Person's name} #like '
p %Q{Person's name} #like "

Upvotes: 1

Timbinous
Timbinous

Reputation: 2983

The reverse holds true too. If I know i'm going to be using double quotes, i'll use single quotes to wrap my string in. However, it may be best to use double quotes at all times , if you use other languages, since single quotes usually just reference a character. It gets annoying modifying my c# code because I said var x = 'hello world';

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160181

No.

But wouldn't just running it have answered that?

Upvotes: 2

Related Questions