Closed triskweline closed 3 years ago
Just a side note: You can use rubocop -a
to let rubocop do the correcting for you (reviewing the corrections should still be done though).
I don't like the idea of not enforcing a style for something so ubiquitous as strings when we go to the length of having something like rubocop at all.
That said, I too would much prefer the "always double quote" style, but I would also prefer the current state over disabling the cop completely.
You can set the style in your rubocop.yml
:
Style/StringLiterals:
EnforcedStyle: single_quotes
or
Style/StringLiterals:
EnforcedStyle: double_quotes
So it would be consistent in the whole project and we added makandra_rubocop for more consistency within a project.
I agree with @kratob on using a preferred style over disabling the cop.
While I personally prefer single quotes, either style is fine for me.
Side note: The idea behind preferring single quotes is not performance, but avoiding double-quoted strings' behavior like unexpected "#@foo"
interpolation and numerous escape sequences you need to waste brain cycles on.
That being said, nobody ever uses "#@foo"
for string interpolation (or would realize because of syntax highlighting) and I've almost never wanted \n
to not mean line break.
There are still situations where one needs to choose the appropriate quote because of properties of the string. E.g. a single-quoted string containing a single quote needs to escape it or use double quotes instead. Double-quoted strings will interpolate #{...}
and \n
, whereas single-quoted strings don't. I believe this is not about saving brain cycles, but a matter of accustoming to a default.
Since Rubocop is all about agreeing on a common code style, I vote for keeping the string literals cop. I have no preference for the enforced quote style.
I will close this issue, as most of us are against this change globally. Please reopen it in case I missed something.
We probably still want discuss if double quotes should become the preferred default style. I will open a new issue for that.
Currently makandra-rubocop complains when I use double quotes in strings without interpolation (StringLiterals cop). I vote that makandra-rubocop should always allow any kind of quote without prejudice.
Disallowing double quotes is one of the oldest cargo cults in Ruby that needed to die ten years ago. It's often justified with hand-wavy arguments about performance that have all been disproven. I don't want to waste brain cycles on thinking about which quote to use every time I make a string.
Prior art
Other tools have also stopped following the single quote cult: