Rubocop is a static code analysis tool for Ruby. ruby-style-guide , point out the modifications and improvements of the source code.
In addition, misalignment of indentation, unnecessary spaces, line breaks, etc. are automatically corrected by typing a specific command, so you can format it into a code that is easy for you and others to read. can. *Settings are decided from the perspective of beginners.
gem'rubocop', require: false
bundle exec rubocop
When you run it, you should get output like this:
These are the fixes that Rubocop has detected. However, if you follow all the default rules, you will have too many points to consider when writing code, so you can set rules that you and your team can accept.
Customize your settings
In the application directory Create a file called .rubocop.yml and write the settings in it.
AllCops: # Set directories to exclude. For example, schema and migration files are rarely rewritten, so they should not be detected. Exclude: -bin/* -db/schema.rb -node_modules/**/* -db/migrate/* -vendor/**/* #Check for Rails Rails: enabled: true Disable # "Missing top-level class documentation comment." Style/Documentation: Enabled: false Disable # "Prefer single-quoted strings when you don't need string interpolation or special symbols." Style/StringLiterals: Enabled: false # "Line is too long" disabled Metrics/LineLength: Enabled: false #'frozen_string_literal: true' disabled Style/FrozenStringLiteralComment: Enabled: false
Various settings are possible. Default settings will be used as a reference to play around with.
After setting, again
bundle exec rubocop
Then you can see that 72files is 56files, the number of corrections is less than before.
bundle exec rubocop --auto-gen-config
A file named .rubocop.todo.yml is automatically generated. As a result, all the modifications will be considered invalid temporarily. If you run rubocop here, there will be no corrections.
# 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 this description is deleted, the invalidated modifications of the relevant part will be restored.
I will correct it immediately,
Cop supports --auto-correct.
If there is a description, Rubocop will automatically correct it with the following command.
bundle exec rubocop -a
This time, the gem description in Gemfile is not in alphabetical order, but it is rearranged in the correct order.
1.Install gem and customize the setting to
bundle exec rubocop --auto-gen-config,
rubocop.todo.yml #### **`.`**
- Delete one of the statements written in
rubocop.todo.yml #### **` and correct it.`**
Repeat step 4.3.
In the actual scene, it seems that it is automated and you will not modify it much yourself, Since I am still a beginner, I am improving one by one and checking the terms, I try to be an engineer who can write easy-to-read code from the beginning.