Use RuboCop to check the conformity of your Ruby code with the style guide

Image for post

Are you writing code that follows the coding conventions? In the first place, I think most businesses might not even have their own coding conventions. Of course, I can’t say that setting and following coding conventions is always a good thing no matter what, but since the conventions themselves are not set by a corporate unit but at the community level, why don’t you try taking a look?

RuboCop is a Ruby source code analysis tool that automatically checks if your code conforms to the Ruby style guide from the Ruby community (English/Japanese), or other style guides (such as MeowCop, which our company provides).

Installing RuboCop

If you already have ruby and can use RubyGems, installation is extremely simple. 0.48.1 was installed in May 2017.

$ gem install rubocop
Fetching: rainbow-2.2.2.gem (100%)
Building native extensions.  This could take a while...
Successfully installed rainbow-2.2.2
Fetching: ast-2.3.0.gem (100%)
Successfully installed rubocop-0.48.1
7 gems installed

How to use RuboCop

To use RuboCop, simply move to the Ruby project you would like to check and execute the rubocop command.

cd path/to/your/project

When you do this, the check will be carried out on the .rb file or Gemfile, and the results will be output.

$ rubocop 
Inspecting 35 files


ncmb-ruby-client.gemspec:12:24: C: Use %q only for strings that contain both single quotes and double quotes.
  spec.description   = %q(A simple Ruby client for the nifty cloud mobile backend REST API)

About options

RuboCop’s options are as follows. RuboCop can basically be used without options, but when you would like to change its behaviour, you can use the following options. There is a way to use  to generate settings files if you would like to create them as you go. When you use , a settings file (.rubocop_todo.yml) will be created in which all the settings entries in which RuboCop has detected violations are deactivated. As the name suggests, you can use this file as a to-do list and gradually activate each of these settings according to your needs in order to improve your code.

On settings

You can fine-tune RuboCop’s settings while using it for analysis. To do so, create a file named  in the project root and describe the settings within that file. For example, there is a Cop known as  that configures the treatment of half-width spaces within hash literals. If you want to configure this Cop to require spaces immediately after the beginning of braces and immediately before their end, but to not require spaces when the braces are empty, simply do as follows.

  EnforcedStyle: space
  EnforcedStyleForEmptyBraces: no_space

For more details on configuration, please refer to Configuration — RuboCop: The Ruby Linter that Serves and Protects.

On compatibility

RuboCop can be used with versions 2.0 to 2.4 of the regular ruby (the so-called MRI), as well as JRuby 9 and above and Rubinius 2 and above. It cannot be used with Ruby 1.9 and below, but isn’t that barely used anymore? We think RuboCop can be used without problems with any typical Ruby project today.

On expansion

RuboCop is not just a normal Ruby project, but can also be used for Rails projects. Rules specific to Rails have been categorised under Rails Cops. These can be activated through the aforementioned settings files and command-line options.

With RuboCop, you can instantly determine if the code you have written confirms to the community’s style guide. Not conforming doesn’t mean there’ll be bugs, but it would surely be better to carry out development in accordance with the style guide, at least when making your library public. Doing this makes your code easier to understand and makes it well-polished.

You can install RuboCop locally and execute it when necessary, but if a whole development team is using it, it’s probably troublesome for each person to execute it and run checks individually. We provide a service called Sider, which automatically runs source-code reviews in the cloud; of course, it’s compatible with RuboCop as well. It is coordinated with GitHub, so when you push your code to the Git repository, the check begins automatically. Please experience the goodness of RuboCop with Sider.

Sider — CI for Building Clean Code and Culture

More articles about RuboCop

Aki Asahara

CEO of Sider. Aki joined Fixstars in 2008 and served major clients such as the US Airforce, MIT, USC, Toyota, and Hitachi High-technologies. After his successful tenure, he was appointed CEO of US operations in 2012. He was appointed CEO of Sider in 2019. He holds a Ph.D. in Astrophysics from Kyoto University and is a Certified Scrum Master.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.