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).
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 rubocop
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 .CCCCC.C...CWCWWCCCCWWCCCWWW..WWCCC Offenses: 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) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ :
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
--auto-gen-config to generate settings files if you would like to create them as you go. When you use
--auto-gen-config, 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.
You can fine-tune RuboCop’s settings while using it for analysis. To do so, create a file named
.rubocop.yml in the project root and describe the settings within that file. For example, there is a Cop known as
SpaceInsideHashLiteralBraces 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.
Style/SpaceInsideHashLiteralBraces: EnforcedStyle: space EnforcedStyleForEmptyBraces: no_space
For more details on configuration, please refer to Configuration — RuboCop: The Ruby Linter that Serves and Protects.
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.
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.
More articles about RuboCop
- Trying the Rails or Lint options in RuboCop
- Using RuboCop for quick automated code reviews with Docker
- Analyzing code with RuboCop on a Ruby on Rails project
- Extract Hidden Coding Styles to .rubocop.yml with Gry
- Let’s start using RuboCop! The release of MeowCop, the best configs for introducing RuboCop!
- Educate RuboCop, try out rubocop.yml which prioritize the speed to the rules