by Shadae Holmes, cohort 1 student. Read the full post on her blog.
The RuboCop gem is a ruby community approved, code style analyzer. We have used it in a few projects to keep in line with recommended ruby style. I usually start by using RuboCop from the beginning of the project. Upon doing this, right away there are errors stating “Prefer single-quoted strings when you don’t need string interpolation or special symbols”. That’s correct, Rails when initialized breaks more than a few Ruby syntax errors right off the bat. While this is a valid style error this is on the bottom of my list of things that need to be done.
My method of working on style from the beginning allows me to fix the generated errors from rails from the beginning. But as many people have said in class, sometimes you just don’t know when you might need to interpolate a string, and since it is a stylistic choice, and will not have any effect on the code I sometimes choose to ignore it.
This week we are starting our projects by refactoring an old project of a group of classmates in which RuboCop and other awesome gems like Flog and Brakeman to analyze the complexity and syntax of the project, and to potentially to refactor it. When it comes to refactoring the errors from RuboCop of course my favorite error often pops up.
This is code showing rubocop failing with double string
test/controllers/user_controller_test.rb:14:8: C: Prefer single-quoted strings when you don't need string interpolation or special symbols. test "should get edit" do ^^^^^^^^^^^^^^^^^ test/controllers/user_controller_test.rb:19:8: C: Prefer single-quoted strings when you don't need string interpolation or special symbols. test "should get destroy" do ^^^^^^^^^^^^^^^^^^^^ test/controllers/user_controller_test.rb:24:8: C: Prefer single-quoted strings when you don't need string interpolation or special symbols. test "should get index" do ^^^^^^^^^^^^^^^^^^ test/controllers/user_controller_test.rb:29:8: C: Prefer single-quoted strings when you don't need string interpolation or special symbols. test "should get show" do ^^^^^^^^^^^^^^^^^ 92 files inspected, 1137 offences detected
I looked into how change at least hide the “Prefer single-quoted strings when you don’t need string interpolation or special symbols” error. Because searching and replacing all double quotes in the project to single quotes would most likely break more things in the end, I decided it was the lowest error that could be generated. To disregard this error you have to first create a .rubocop.yml in your projects main file. The . needs to be in front of the file, and it will ask you if you want to add a system file as they are only named accordingly. Then change the added .rubocop.yml file to :
### # Configuration for rubocop # in .rubocop.yml # Most of these are disabling existing cops, primarily # due to a smattering of different styles and loose # guidlines for contributions. # # Any of these may be changed. StringLiterals: Enabled: false
The singular change to the .yml file decreases the offenses by about 25%. thisallows me to focus on other parts of the code that need more detail and attention in refactoring. In the end if I want to come back to it I can.
792 files inspected, 735 offences detected