rubocop snippets

How to introduce Rubocop in your project

Tagged rubocop, style-guide, ruby  Languages ruby

Installation

Edit Gemfile:

group :development do
  gem 'rubocop' # Style guide checker
  gem 'guard-rubocop' # Rubocop plugin for Guard
end

Install:

$ bundle

Usage

Run Rubocop:

$ rubocop --auto-gen-config

Tweaking

Edit .rubocop.yml:

inherit_from: .rubocop_todo.yml

Introduce new rules gradually in priority order by enabling and moving rules from .rubocop_todo.yml to .rubocop.yml.

You can also use the Ruby style-guide's rubocop configuration: https://github.com/bbatsov/rubocop/blob/master/config/default.yml

Workflow integration

Run:

$ guard init rubocop

Edit Guardfile:

guard :rubocop, all_on_start: false, cli: ['--format', 'clang', '--rails'] do
  watch(%r{.+\.rb$})
  watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
end

Editor Integration

Optionally, integrate Rubocop with your editor: * Textmate * Vim

CI integration

See How to introduce rubocop and integrate into Jenkins.

Running Rubocop on commit, changes, or branch

Tagged branch, git, rubocop  Languages 
  • To RuboCop the current commit
git diff-tree --no-commit-id --name-only -r HEAD --diff-filter AMT | xargs bundle exec rubocop
  • To RuboCop the working tree changes
git diff --name-only --diff-filter AMT | xargs bundle exec rubocop
  • To RuboCop all of the changes from the branch
git diff --name-only master --diff-filter AMT | xargs bundle exec rubocop

From https://about.gitlab.com/handbook/tools-and-tips/rubocop/

Also see: