Rails 7.2 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 7.2

Before Rails 7.2, we had to manually integrate the Rubocop gem into 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 files. 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 7.2

Now, Rails 7.2 comes with the 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 the rubocop gem in our Rails application, we can skip it by passing the --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