ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina
RuboCop is a static code analysis tool that checks whether ruby code is "written according to coding standards".
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
Add as below.
Gemfile
group :development do
...
gem 'rubocop-airbnb'
end
Terminal
$ bundle install
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
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.
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