[Ruby on Rails] Code check using Rubocop-airbnb

Development environment

ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina

What is RuboCop?

RuboCop is a static code analysis tool that checks whether ruby code is "written according to coding standards".

What is rubocop-airbnb?

This is an analysis created by airbnb, and this time we will use this to check the code. git is as follows. https://github.com/airbnb/ruby/tree/master/rubocop-airbnb

Installation of "Rubocop-airbnb"

Add as below.

Gemfile


group :development do

...

  gem 'rubocop-airbnb'
end

Terminal


$ bundle install

Creating a configuration file

Under the application folder Create ".rubocop.yml" and ".rubocop_airbnb.yml" files. By creating these, specify the files and folders that you do not want to analyze the code with rubocop.

:.rubocop.yml


inherit_from:
  - .rubocop_airbnb.yml

AllCops:
  Exclude:
    - 'db/**/*'
    - 'bin/*'
    - 'config/environments/*'
    - 'config/application.rb'
    - 'config/initializers/*'
    - 'config/spring.rb'
    - 'lib/tasks/*'
    - 'vendor/**/*'
    - 'path/ruby'

:.rubocop_airbnb.yml


require:
  - rubocop-airbnb

Check the code with Rubocop

In the terminal, go to the application folder and execute the following.

Terminal


$ bundle exec rubocop --require rubocop-airbnb

...

50 files inspected, 150 offenses detected

It will be displayed like this. It means that we checked 50 files and found 150 coding violations. If the number is huge like this, it is difficult to check each one, so execute the following.

Terminal


$ bundle exec rubocop --require rubocop-airbnb -a

...

50 files inspected, 150 offenses detected, 150 offenses corrected

He fixed all 150 in this way. If the correction is still not possible, it will be checked visually.

Summary

In this way, you can easily fix the code automatically. It's very convenient, so please use it.

Also, on twitter, technologies and ideas that have not been uploaded to Qiita are also uploaded, so I would be grateful if you could follow me. Click here for details https://twitter.com/japwork

Recommended Posts

[Ruby on Rails] Code check using Rubocop-airbnb
Notes on using FCM with Ruby on Rails
[Ruby on Rails] Image slideshow using Skippr
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
[Ruby on Rails] Common processing between controllers (using concaves)
[Rails] Test code using Rspec
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on Rails environment construction using VirtualBox, Vagrant, cyberduck
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
Try using the query attribute of Ruby on Rails
Ruby on Rails Email automatic sending function setting (using gmail)
<Dot installation> Introduction to Ruby on Rails5 Source code comparison
[Ruby on Rails] Automatically enter the address from the zip code
[Ruby on Rails] Easy scroll animation of javascript (using ScrollReveal.js)
[Ruby on Rails] Infinite scrolling using gem kaminari and jscroll
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
Install Ruby 2.5 on CentOS 7 using SCL
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
Check the root on the Rails browser
[Ruby on Rails] Stop "looping until ..."
Understand code coverage with Rspec, the Ruby on Rails test framework
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Creating an inquiry form
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] View test with RSpec