Lint your Ruby code with Overcommit and static analysis tools

Using Git hooks to control code quality for Ruby, Rails and Chef

Kirill Shevchenko
Jul 15, 2017 · 5 min read

What is Linter?

Linter is a static analysis tool such as RuboCop. It checks the written code and makes suggestions based on pre-defined rules by community-driven coding style guides and common idioms.

Most definitions of linter default behavior can be changed for the needs of your team and project via various configuration options.

Why linting and following style guides is important?

There are many reasons to lint the code:

  1. It maintains code consistency
  2. It helps catch unnecessary code
  3. It helps avoid conflicts between developers and also to remember about the conventions
  4. It helps optimize performance and avoid problems with security

Don’t forget to lint with Git hooks and Overcommit

Git hooks are scripts that execute before or after actions such as: commit, push, and receive. Git hooks are available out of the box.

From my experience, pre-commit hooks are perfect for automated running the enabled linters to check new changes.


Or using bundler

Initialize configuration file:

And override default configuration under your needs.

example of .overcommit.yml file

Before starting development overcommit should be installed the following commands:

Adding RuboCop to your project

Add RuboCop to project Gemfile:

And now you can check current code by just typing:

Apart from reporting problems in your code, RuboCop can also automatically fix some of the problems for you.

Also feel free to configure it for your preferences. Init .rubocop.yml into project folder and override needed options.

Initialize configuration file:

example of .rubocop.yml file

Finally, enable pre-commit hook into overcommit.

The analyzer will be launched automatically for new code changes, which you will try to add to the new commit.

Most useful tools to linting of Ruby code


It is well known that the Ruby language not the fastest. Fasterer helps us to make our code faster by some speed improvements based on fast-ruby.

Run analyzer:

Initialize configuration file:

Use list of available definitions to override them.

example of .fasterer.yml file

Enable into overcommit config:


Check Gemfile.lock for installation from non-secured connections and the presence of versions of gems with vulnerabilities.

Manual running:

Enable into overcommit config:

Example of output before trying commit:


Reek is a tool that examines Ruby classes, modules and methods and reports any Code Smells it finds.

Reek currently includes checks for some aspects of Control Couple, Data Clump, Feature Envy, Large Class, Long Parameter List, Simulated Polymorphism, Too Many Statements, Uncommunicative Name, Unused Parameters and more. See the Code Smells for up to date details of exactly what Reek will check in your code.

It also one of the most popular analyzer that can help growth quality of code base.

Can be configured for example via config.reek file by overriding huge list of default options.

example of config.reek file

Enable into overcommit config:

Static analysis tools for Rails applications


rails_best_practices is a code metric tool to check the quality of Rails code.

Helps to find unused methods, missing indexes into database tables and many other things.

Run from root app directory:

Or for HTML output:

Enable into overcommit:

The downside is that, rails_best_practices with pre-commit checks whole code of projects instead of only new changes.


One more tool which checks applications for security vulnerabilities — brakeman.

It can be integrated with overcommit by pre-hush hook.

rails_schema_up_to_date pre-commit hook

Overcommit includes useful option which can helps check to see whether the schema file is in line with the migrations.

Linting for Chef

Foodcritic is a tool which helps writing quality and safer Chef cookbooks Out of the box Foodcritic contains over 70 cookbook rules, and plugin system for writing your own rules.

Monolithic repo mode

Add to Gemfile of Chef project:

And it will check your custom cookbooks into site-cookbooks folder, roles and environments files.

Single cookbook repo mode

The default. Use this if your repository contains just a single cookbook. To get this to work well, you need to write into configuration something like:

berksfile_check pre-commit hook

If you are using Berkshelf to manage cookbooks, it option can check that local Berksfile.lock matches Berksfile when either changes.

Other tools — RubyCritic is a gem that wraps around static analysis gems such as Reek, Flay and Flog to provide a quality report of your Ruby code. Fukuzatsu is a tool for measuring code complexity in Ruby class files. Its analysis generates scores based on cyclomatic complexity algorithms. — Quick automated code review of your changes.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store