Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which style of Ruby string quoting do you favour?

Tags:

string

ruby

Which style of Ruby string quoting do you favour? Up until now I've always used 'single quotes' unless the string contains certain escape sequences or interpolation, in which case I obviously have to use "double quotes".

However, is there really any reason not to just use double quoted strings everywhere?

like image 493
John Topley Avatar asked Nov 10 '08 21:11

John Topley


2 Answers

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:

Mirror of Site - https://web.archive.org/web/20160310224440/http://rors.org/2008/10/26/dont-escape-in-strings

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

like image 185
Dejan Simic Avatar answered Oct 11 '22 01:10

Dejan Simic


I always use single quotes unless I need interpolation.

Why? It looks nicer. When you have a ton of stuff on the screen, lots of single quotes give you less "visual clutter" than lots of double quotes.

I'd like to note that this isn't something I deliberately decided to do, just something that I've 'evolved' over time in trying to achieve nicer looking code.

Occasionally I'll use %q or %Q if I need in-line quotes. I've only ever used heredocs maybe once or twice.

like image 40
Orion Edwards Avatar answered Oct 11 '22 02:10

Orion Edwards