[RUBY] [Rails] Introduction of Rubocop by beginners

Introduction

Rubocop is a Ruby static code analysis tool. ruby-style-guide will point out the corrections and improvements in the source code.

Indentation deviations, unnecessary spaces, line breaks, etc. are automatically corrected by typing a specific command, so you can format the code so that it is easy for you to read as well as for others. can.

Introduction

gemfile


gem 'rubocop', require: false

Terminal


bundle install

Try using

Terminal


bundle exec rubocop

When run, you will get output similar to the following: スクリーンショット 2020-07-10 8.06.47.png

These are the fixes detected by Rubocop. However, if you follow all the default rules, you'll have too much to worry about when writing code, so you can set the rules you and your team allow.

Customize settings

In the application directory Create a file called .rubocop.yml and write the settings in it.

.rubocop.yml


AllCops:
  #Set directories to exclude. For example, schema and migration files are rarely rewritten, so they are not detected.
  Exclude:
    - bin/*
    - db/schema.rb
    - node_modules/**/*
    - db/migrate/*
    - vendor/**/*
  #Check for Rails
  Rails:
    enabled: true

# "Missing top-level class documentation comment."Disable
Style/Documentation:
  Enabled: false

# "Prefer single-quoted strings when you don't need string interpolation or special symbols."Disable
Style/StringLiterals:
  Enabled: false

# "Line is too long"Disable
Metrics/LineLength:
  Enabled: false

#'frozen_string_literal: true'Disable
Style/FrozenStringLiteralComment:
  Enabled: false

Various settings such as are possible. I will play around with it by referring to Default settings.

After setting, again

Terminal


bundle exec rubocop
スクリーンショット 2020-07-10 8.29.43.png Then, you can see that 72files is 56files, which is less than before.

To fix

Terminal


bundle exec rubocop --auto-gen-config

A file called .rubocop.todo.yml is automatically generated. As a result, all corrections will be temporarily invalidated. If you run rubocop here, there will be no corrections.

ruby:.rubocop.todo.yml


# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation, Include.
# Include: **/*.gemfile, **/Gemfile, **/gems.rb
Bundler/OrderedGems:
  Exclude:
    - 'Gemfile'

As an example, there is the above description. If you delete this description, the invalidated corrections of the relevant part will be restored. I will fix it immediately, but

Cop supports --auto-correct.


 If there is a description, Rubocop will automatically correct it with the following command.


#### **`Terminal`**
```python

bundle exec rubocop -a

This time, the gem description in the Gemfile is not in alphabetical order, but it is rearranged in the correct order.

Summary

  1. Install gem and customize the settings to `` `.rubocop.yml```
  2. Execute `bundle exec rubocop --auto-gen-config``` and Generate `.rubocop.todo.yml```.
  3. Delete and correct one description in .rubocop.todo.yml Repeat 4.3.

In the actual field, it is automated and it seems that you will not fix it yourself, I'm still a beginner, so I'll improve each one and check the rules. I try to be an engineer who can write easy-to-read code from the beginning.

Recommended Posts

[Rails] Introduction of Rubocop by beginners
[Rails] Introduction of PAY.JP
A review of the code used by rails beginners
[Ruby on Rails] Introduction of initial data
[Rails] Temporary retention of data by session
Summary of Docker understanding by beginners ② ~ docker-compose ~
What Rails beginners learned by solving errors
Rails [For beginners] Implementation of comment function
Explanation of Ruby on rails for beginners ①
[Vue.js] Implementation of menu function Vue.js introduction rails6
Introduction of pay.jp
Introduction of milkode
[Rails 6] cocoon_ introduction
[Ruby on Rails] Until the introduction of RSpec
Summary of basic knowledge of Rails acquired by progate
Summary of Docker understanding by beginners ① ~ docker run -p ~
[Introduction to Java] Basics of java arithmetic (for beginners)
[Ruby / Rails] Inactivate Lint of update_all with Rubocop
[Rails 6] Validation by belongs_to
RxJava Beginner's Recommendation of RxJava
List of beginners (List) memo
Rails Tutorial/Significance of Indexing
Introduction to Java for beginners Basic knowledge of Java language ①
[Rails] devise introduction method
Introduction of user authentication
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
[Rails 6] Rubocop warning list
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Enum] Let's improve the readability of data by using rails enum
[Rails] Register by attribute of the same model using Devise
Ruby on Rails for beginners! !! Summary of new posting functions
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
[Rails] How to display the list of posts by category
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
What beginners can learn by creating a simple Rails API
Rails implementation of ajax removal
[Java] Beginner's understanding of Servlet-②
[Docker] Introduction of basic Docker Instruction
Utilization of Rails Boolean type
[Java] Beginner's understanding of Servlet-①
[Rails 6] Implementation of search function
[For super beginners] DBUnit super introduction
[Rails] Implementation of category function
Object-oriented summary by beginners (Java)
(For beginners) [Rails] Install Devise
[For super beginners] Ant super introduction
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[For super beginners] Maven super introduction
Introduction to RSpec-Everyday Rails Summary-
Rails singular resource routing by resource
[Rails] Suppress unnecessary SQL by utilizing the cache control of the association
[RSpec on Rails] How to write test code for beginners by beginners
[Rails 6] Implementation of new registration function by SNS authentication (Facebook, Google)
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
[Java] Appropriate introduction by the people of Tempa Java Part 0 (Code rules)
Replace preview by uploading by clicking the image in file_field of Rails
Summary of Docker understanding by beginners ③ ~ Until proxying API using nginx ~
[rails] Edit of view page created by devise is not reflected