Let’s get back to Ruby! Have you ever faced CI build failures? For those of you who have Luca Guidi prepared tricks to avoid them. Would you like to explore practical tips for Rubocop workflow?
Rubocop is a Ruby static code analyzer, based on the community Ruby style guide. This linting tool for checking all Ruby source files in the current directory is one of the most used Ruby gems of all the time.
Bare minimum group of settings
# Please keep AllCops, Bundler, Layout, Style, Metrics groups and then order cops
# alphabetically
#
# References:
# * https://github.com/bbatsov/ruby-style-guide
# * https://rubocop.readthedocs.io/
AllCops:
DisplayCopNames: true
DisplayStyleGuide: true
ExtraDetails: false
TargetRubyVersion: 2.5
Integrate with your editor
Configure it to show not just syntax errors, but also Rubocop violations.
Integrate the autocorrect option with your editor
function! RubocopAutocorrect()
execute "!rubocop -a " . bufname("%")
call SyntasticCheck()
endfunction
map <silent> <Leader>cop :call RubocopAutocorrect()<cr>
Pre-commit hook
Install a Git pre-commit hook to handle situations when you don’t edit the file, yet still get CI failures.
Copy the following code into .git/hooks/pre-commit
and ensure this file is an executable.
#!/bin/bash
git status -s | grep -E 'A|M|R' | awk '{print $2}' | xargs rubocop
We hope these tips will make you not afraid of CI build failures in Rubocop anymore!
Read the details here.