More guard-rubocop (autocorrect and configure)
An update from my last post, and a couple more tips and questions for guard-rubocop:
In the directory where you install guard-rubocop, you also want to use rvm to specify a version of ruby and specific gemset for that directory. DO THIS BEFORE INSTALLING GUARD-RUBOCOP. So, go to whatever directory you want guard-rubocop installed in, and type:
echo '2.1.2' > .ruby-version
echo 'name_of_directory' > .ruby-gemset
Now to make these files take effect, change out of this directory and back in, like so:
This will generate wrappers for the direcotry and, you can now install guard-rubocop as per my last post, and it should fix the problem I faced before of needing to initialize guard every time i wanted to run it.
A few things I learned:
You can specify a directory or file for rubocop to inspect, instead of all files in that directory. Just run:
Rubocop can also autocorrect some offenses like so:
In addition, I finally figured out how to configure guard-rubocop, so I can specify which conventions (called cops) I want rubocop to notify me about, and how to modify some of the rules. After several failed attempts at following the guide at https://github.com/bbatsov/rubocop, I finally found the sought after /config/default.yml file. Alright, so here is how to configure guard-rubocop:
1) First follow all steps in my previous blog post and the beginning of this post.
2) locate the config/default.yml file. I did this by opening terminal and searching from my home directory by typing
$ find ~ -name "default.yml"
This gave me the following list:
These are all the locations where I installed guard-rubocop, and where the default.yml file is. I suppose you could use any of these files, because I assume they are all the same. I chose the one in the directory ruby-2.1.2, rather than any of the ruby-2.1.2@some_other_directory, so I copied the full path to the file on my clipboard (you need to copy your own path to that file, not mine):
If your terminal did not return any results for the search, go back and try installing guard-rubocop again. Phew!
3) Ok, now use the terminal to go to the directory where you want to install the modifications to guard-rubocop, and create a .rubocop.yml file. My preferred method of this to to type
This will create the file, and open a built in text editor where you can add text to the file. Add the following text (after the inherit_from: part, you can paste the path you copied before):
you can then quit the text editor by typing ctrl+x, pressing ‘y’ to save the changes, and pressing enter. You’ve now created the .rubocop.yml file, which list the modifications, or deviations from the default. Any lines added in this file will override the ones in default.yml.
The cops in the example above do the following:
This changes the maximum line length that rubocop allows. So I can now have lines up to 100 characters, instead of 80.
This stops rubocop from complaining about double quotes (which it does if you don’t use string interpolation or special characters).
This changes the prefered style of string formatting to ‘percent’ (it is format by ‘default’). Before I included this, rubocop would complain whenever I used string formatting like this:
Now it doesn’t complain (although I imagine it will now complain when you use sprintf or format)
You can peruse the default.yml file here to find other modification you might want to try. You can always go back to the .rubocop.yml file by typing
nano .rubocop.yml and editing the cops listed.
A question I still have is: What is “guard” vs. what is “rubocop”? I’m still new to the whole gem thing…