Hacker News new | ask | show | jobs
by monoideism 2040 days ago
> would rather only use double quotes when necessary, otherwise use single quotes

So in that case, they're not used randomly according to the individual user's preference. There's a consistent rule. It's helpful for readability, too. And not sure about Rubocop in Ruby, but many linters in other languages can pick up on these different applications.

2 comments

Generally in ruby/rails, the people in favor of double quotes would use double quotes all the time.

Rubocop would have you use singles for non interpolated and double for interpolated.

I get that there is a potential to catch some kind of error where you use double quotes on a string that will interpolated something which it wouldn't if it were in single quotes. In 12 years, I've never had that happen. I have, however, had it happen dozens of times where I changed something from interpolated to not and then I have to bounce over to change the quotes. This happens a lot and is annoying.

It's also an eyesore when you have an array of messages, each neatly aligned, but some are interpolated and some aren't, so the quotes change.

In Ruby, knowing that a string has interpolation is easy enough by seeing the #{} in the middle of it. It's hard to miss, really.

The single/double quote cop which is turned on by default is also counter to the existing style of every-string-is-double when it came along.

interpolation isn't the only difference between single- and double-quoted strings. If you need control, etc., characters via backslash escapes, you must use double (or semantic equivalents like %/%Q). If you need non-eyesore strings with literal backslashes, you need single (or semantic equivalents like %q.)

  irb(main):001:0> puts '1\t2',"1\t2"
  1\t2
  1       2
> In Ruby, knowing that a string has interpolation is easy enough by seeing the #{} in the middle of it.

{} are not required for instance variables.

> the people in favor of double quotes would use double quotes all the time.

Fair enough. I'm not much into Ruby but if I were, that's what I would encourage. But I'd be ok with whatever house style, as long as it's consistent.

Rubocop does pick it up by default.