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

and

echo 'name_of_directory' > .ruby-gemset

Now to make these files take effect, change out of this directory and back in, like so:

cd .

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 path/to/file.rb

Rubocop can also autocorrect some offenses like so:

rubocop --auto-correct
or
rubocop -a

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):

/Users/glassbead/.rvm/gems/ruby-2.1.2/gems/rubocop-0.23.0/config/default.yml

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

nano .rubocop.yml

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:

Style/LineLength:
Max: 100

This changes the maximum line length that rubocop allows. So I can now have lines up to 100 characters, instead of 80.

Style/StringLiterals:
Enabled: false

This stops rubocop from complaining about double quotes (which it does if you don’t use string interpolation or special characters).

Style/FormatString:
EnforcedStyle: percent
SupportedStyles:
– format
– sprintf
– percent

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.

Happy policing!

A question I still have is: What is “guard” vs. what is “rubocop”? I’m still new to the whole gem thing…

Thanks to Bill Shannon and Jason Noble for their blog posts about this topic as well.

Advertisements

Tags: , , , , , ,

About Aaron Glasenapp

I am a freelance Web/Rails developer and a hard core recreational mathematician.

2 responses to “More guard-rubocop (autocorrect and configure)”

  1. Jason Noble (@jasonnoble) says :

    Guard (https://github.com/guard/guard) is a Rubygem that provides a command line tool that handles events when the file system is modified (i.e. you save/create a file). Guard has a lot of plugins (called Guards): https://github.com/guard/guard/wiki/List-of-available-Guards.

    So when guard sees something change on the filesystem, it goes through it’s Guardfile to figure out what should happen.

    In our Guardfile:

    1) guard :rubocop do
    2) watch(%r{.+\.rb$})
    3) watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
    4) end

    It basically tells guard that it’s looking for files ending in .rb (i.e. Ruby source files) (Line 1) or files named .rubocop.yml (Line 2). If either of these match (i.e. a Ruby file changed), then Guard tells the Rubocop program to run.

    Rubocop then runs and analyzes all your Ruby code to look for style guide “violations”.

  2. Wendy Brannon says :

    Thanks Aaron, by referring to your post, I was able to have rubocop ignore my string format offenses.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: