Rails 8 adds Rubocop by default to new applications

RuboCop is a static code analyzer also linter and code formatter for the Ruby programming language. It will enforce the guidelines outlined in the community Ruby Style Guide.

It helps developers in ensuring adherence to coding style standards, identifying potential code flaws, and enhancing the overall quality of the codebase. In addition to identifying code problems, RuboCop also automatically corrects those issues.

Developers can adjust rules defined by Rubocop to match project coding standards.

Before Rails 8.0

Before Rails 8 we had to manually integrate Rubocop gem to our project.

We can simply install it like below.

gem install rubocop

or we can add it to the gemfile of the project.

gem 'rubocop', require: false

The behavior of RuboCop can be controlled via the .rubocop.yml configuration file. We can create this file manually and we can put it in the root of our project folder.

Or we can run the below command that will automatically create the rubocop.yml and rubocop.todo.yml file. It is a good idea to use the command below if our codebase has an overwhelming number of offenses.

rubocop --auto-gen-config

All the rules related to code formatting will be defined in the .rubocop.yml file. The Basic structure of the file will look like below.

inherit_from: ../.rubocop.yml

Style/Encoding:
  Enabled: false

Layout/LineLength:
  Max: 99

Now we can run bundle exec rubocop to check the issues with our codebase and bundle exec rubocop -a will automatically correct the offenses.

In Rails 8.0

Now Rails 8 comes with rubocop gem by default. It enforces rules from rubocop-rails-omakase by default.

The rubocop-rails-omakase is an extension of RuboCop specifically tailored for Ruby on Rails applications. It includes additional rules and configurations that are relevant and specific to Rails development

If we don’t want rubocop gem in our Rails application, we can skip it by passing --skip-rubocop flag.

To know more about this please check this PR.

Need help on your Ruby on Rails or React project?

Join Our Newsletter