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?
-
I usually use double quotes unless I specifically need to disable escaping/interpolation.
-
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.
-
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
John Topley : Nice tip, thanks! -
I use single quotes unless I need interpolation, or the string contains single quotes.
However, I just learned the arbitrary delimiter trick from Dejan's answer, and I think it's great. =)
-
I used to use single quotes until I knew I needed interpolation. Then I found that I was wasting a lot of time when I'd go back and have to change some single-quotes to double-quotes. Performance testing showed no measurable speed impact of using double-quotes, so I advocate always using double-quotes.
The only exception is when using sub/gsub with back-references in the replacement string. Then you should use single quotes, since it's simpler.
mystring.gsub( /(fo+)bar/, '\1baz' ) mystring.gsub( /(fo+)bar/, "\\1baz" )
-
I use single quotes unless I need interpolation. The argument about it being troublesome to change later when you need interpolation swings in the other direction, too: You have to change from double to single when you found that there was a # or a \ in your string that caused an escape you didn't intend.
The advantage of defaulting to single quotes is that, in a codebase which adopts this convention, the quote type acts as a visual cue as to whether to expect interpolated expressions or not. This is even more pronounced when your editor or IDE highlights the two string types differently.
I use %{.....} syntax for multi-line strings.
-
Like many programmers, I try to be as specific as is practical. This means that I try to make the compiler do as little work as possible by having my code as simple as possible. So for strings, I use the simplest method that suffices for my needs for that string.
<<END For strings comtaining multiple newlines, particularly when the string is going to be output to the screen (and thus formatting matters), I use heredocs. END %q[Because I strongly dislike backslash quoting when unnecessary, I use %Q or %q for strings containing ' or " characters (usually with square braces, because they happen to be the easiest to type and least likely to appear in the text inside).] "For strings needing interpretation, I use %s."%['double quotes'] 'For the most common case, needing none of the above, I use single quotes.'
My first simple text of the quality of syntax highlighting provided by a program is to see how well it handles all methods of quoting.
0 comments:
Post a Comment