DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

rubocop for code quality and formatting in Rails

🔗 Parent Note

🤔 What is rubocop?

​
📚 rubocop document
​

RuboCop is a Ruby static code analyzer (a.k.a. linter) and code formatter. Out of the box it will enforce many of the guidelines outlined in the community Ruby Style Guide.
​

🤔 How to use?

​
put the .rubocop.yml on app directory and run following command.
​

bundle exec rubocop --config .rubocop.yml
Enter fullscreen mode Exit fullscreen mode

​
then, get the results like this.
​

path/to/file.rb::11.1  C: Layout/TrailingWhitespace: Trailing whitespace detected.
273 files inspected, 1 offenses detected
Enter fullscreen mode Exit fullscreen mode

​
It means that rubocop detects White Space which should be removed at the end of the line at row11, 1char, and the violation is only 1 in 273 files.
​
​

🤔 How to fix it?

​

1. Google with the detected message

​
When you google with Layout/TrailingWhitespace, you'll see following explanation, then fix it.
​

# The line in this example contains spaces after the 0.
# bad
x = 0
​
# The line in this example ends directly after the 0.
# good
x = 0
Enter fullscreen mode Exit fullscreen mode

​

2. Auto correct

use -a option.
​

bundle exec rubocop -a
Enter fullscreen mode Exit fullscreen mode

​
You can find other options on the 📚 document.
​

Top comments (0)