{"id":7560,"date":"2015-07-28T17:41:35","date_gmt":"2015-07-28T17:41:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/07\/28\/bbatsov-rubocop\/"},"modified":"2015-07-28T17:41:35","modified_gmt":"2015-07-28T17:41:35","slug":"bbatsov-rubocop","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/07\/28\/bbatsov-rubocop\/","title":{"rendered":"bbatsov\/rubocop"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/badge.fury.io\/rb\/rubocop.svg\" \/> <img decoding=\"async\" src=\"http:\/\/gemnasium.com\/bbatsov\/rubocop.svg\" \/> <img decoding=\"async\" src=\"http:\/\/travis-ci.org\/bbatsov\/rubocop.svg?branch=master\" \/> <img decoding=\"async\" src=\"http:\/\/img.shields.io\/coveralls\/bbatsov\/rubocop\/master.svg\" \/> <img decoding=\"async\" src=\"http:\/\/codeclimate.com\/github\/bbatsov\/rubocop\/badges\/gpa.svg\" \/> <img decoding=\"async\" src=\"http:\/\/inch-ci.org\/github\/bbatsov\/rubocop.svg\" \/> <img decoding=\"async\" src=\"http:\/\/img.shields.io\/gratipay\/bbatsov.svg\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/raw.github.com\/bbatsov\/rubocop\/master\/logo\/rubo-logo-horizontal.png\" \/><\/p>\n<blockquote>\n<p>Role models are important.<br \/>\n\u2013 Officer Alex J. Murphy \/ RoboCop<\/p>\n<\/blockquote>\n<p><strong>RuboCop<\/strong> is a Ruby static code analyzer. Out of the box it will enforce many of the guidelines outlined in the community Ruby Style Guide.<\/p>\n<p>Most aspects of its behavior can be tweaked via various configuration options.<\/p>\n<p>Apart from reporting problems in your code, RuboCop can also automatically fix some of the problems for you.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/badges.gitter.im\/Join%20Chat.svg\" \/><\/p>\n<p>You can support my work on RuboCop and all my other projects via gratipay.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.rawgit.com\/gratipay\/gratipay-badge\/2.1.3\/dist\/gratipay.png\" \/><\/p>\n<p><strong>This documentation tracks the <code>master<\/code> branch of RuboCop. Some of the features and settings discussed here might not be available in older releases (including the current stable release). Please, consult the relevant git tag (e.g. v0.30.0) if you need documentation for a specific RuboCop release.<\/strong><\/p>\n<h2>Installation<\/h2>\n<p><strong>RuboCop<\/strong>&#8216;s installation is pretty standard:<\/p>\n<pre><code>$ gem install rubocop\n<\/code><\/pre>\n<p>If you\u2019d rather install RuboCop using <code>bundler<\/code>, don\u2019t require it in your <code>Gemfile<\/code>:<\/p>\n<pre><code>gem 'rubocop', require: false\n<\/code><\/pre>\n<h2>Basic Usage<\/h2>\n<p>Running <code>rubocop<\/code> with no arguments will check all Ruby source files in the current directory:<\/p>\n<pre><code>$ rubocop\n<\/code><\/pre>\n<p>Alternatively you can pass <code>rubocop<\/code> a list of files and directories to check:<\/p>\n<pre><code>$ rubocop app spec lib\/something.rb\n<\/code><\/pre>\n<p>Here\u2019s RuboCop in action. Consider the following Ruby source code:<\/p>\n<pre><code>def badName\n  if something\n    test\n    end\nend\n<\/code><\/pre>\n<p>Running RuboCop on it (assuming it\u2019s in a file named <code>test.rb<\/code>) would produce the following report:<\/p>\n<pre><code>Inspecting 1 file\nW\n\nOffenses:\n\ntest.rb:1:5: C: Use snake_case for method names.\ndef badName\n    ^^^^^^^\ntest.rb:2:3: C: Use a guard clause instead of wrapping the code inside a conditional expression.\n  if something\n  ^^\ntest.rb:2:3: C: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &amp;&amp;\/||.\n  if something\n  ^^\ntest.rb:4:5: W: end at 4, 4 is not aligned with if at 2, 2\n    end\n    ^^^\n\n1 file inspected, 4 offenses detected\n<\/code><\/pre>\n<p>For more details check the available command-line options:<\/p>\n<pre><code>$ rubocop -h\n<\/code><\/pre>\n<table>\n<tr>\n<th>Command flag<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><code>-v\/--version<\/code><\/td>\n<td>Displays the current version and exits.<\/td>\n<\/tr>\n<tr>\n<td><code>-V\/--verbose-version<\/code><\/td>\n<td>Displays the current version plus the version of Parser and Ruby.<\/td>\n<\/tr>\n<tr>\n<td><code>-F\/--fail-fast<\/code><\/td>\n<td>Inspects in modification time order and stops after first file with offenses.<\/td>\n<\/tr>\n<tr>\n<td><code>-d\/--debug<\/code><\/td>\n<td>Displays some extra debug output.<\/td>\n<\/tr>\n<tr>\n<td><code>-D\/--display-cop-names<\/code><\/td>\n<td>Displays cop names in offense messages.<\/td>\n<\/tr>\n<tr>\n<td><code>-c\/--config<\/code><\/td>\n<td>Run with specified config file.<\/td>\n<\/tr>\n<tr>\n<td><code>-f\/--format<\/code><\/td>\n<td>Choose a formatter.<\/td>\n<\/tr>\n<tr>\n<td><code>-o\/--out<\/code><\/td>\n<td>Write output to a file instead of STDOUT.<\/td>\n<\/tr>\n<tr>\n<td><code>-r\/--require<\/code><\/td>\n<td>Require Ruby file (see Loading Extensions).<\/td>\n<\/tr>\n<tr>\n<td><code>-R\/--rails<\/code><\/td>\n<td>Run extra Rails cops.<\/td>\n<\/tr>\n<tr>\n<td><code>-l\/--lint<\/code><\/td>\n<td>Run only lint cops.<\/td>\n<\/tr>\n<tr>\n<td><code>-a\/--auto-correct<\/code><\/td>\n<td>Auto-correct certain offenses. <em>Note:<\/em> Experimental &#8211; use with caution.<\/td>\n<\/tr>\n<tr>\n<td><code>--only<\/code><\/td>\n<td>Run only the specified cop(s) and\/or cops in the specified departments.<\/td>\n<\/tr>\n<tr>\n<td><code>--except<\/code><\/td>\n<td>Run all cops enabled by configuration except the specified cop(s) and\/or departments.<\/td>\n<\/tr>\n<tr>\n<td><code>--auto-gen-config<\/code><\/td>\n<td>Generate a configuration file acting as a TODO list.<\/td>\n<\/tr>\n<tr>\n<td><code>--show-cops<\/code><\/td>\n<td>Shows available cops and their configuration.<\/td>\n<\/tr>\n<tr>\n<td><code>--fail-level<\/code><\/td>\n<td>Minimum severity for exit with error code. Full severity name or upper case initial can be given. Normally, auto-corrected offenses are ignored. Use <code>A<\/code> or <code>autocorrect<\/code> if you\u2019d like them to trigger failure.<\/td>\n<\/tr>\n<\/table>\n<h3>Cops<\/h3>\n<p>In RuboCop lingo the various checks performed on the code are called cops. There are several cop departments.<\/p>\n<p>You can also load custom cops.<\/p>\n<h4>Style<\/h4>\n<p>Most of the cops in RuboCop are so called style cops that check for stylistics problems in your code. Almost all of the them are based on the Ruby Style Guide. Many of the style cops have configurations options allowing them to support different popular coding conventions.<\/p>\n<h4>Lint<\/h4>\n<p>Lint cops check for possible errors and very bad practices in your code. RuboCop implements in a portable way all built-in MRI lint checks (<code>ruby -wc<\/code>) and adds a lot of extra lint checks of its own. You can run only the lint cops like this:<\/p>\n<pre><code>$ rubocop -l\n<\/code><\/pre>\n<p>The <code>-l<\/code>\/<code>--lint<\/code> option can be used together with <code>--only<\/code> to run all the enabled lint cops plus a selection of other cops.<\/p>\n<p>Disabling any of the lint cops is generally a bad idea.<\/p>\n<h4>Metrics<\/h4>\n<p>Metrics cops deal with properties of the source code that can be measured, such as class length, method length, etc. Generally speaking, they have a configuration parameter called <code>Max<\/code> and when running <code>rubocop --auto-gen-config<\/code>, this parameter will be set to the highest value found for the inspected code.<\/p>\n<h4>Rails<\/h4>\n<p>Rails cops are specific to the Ruby on Rails framework. Unlike style and lint cops they are not used by default and you have to request them specifically:<\/p>\n<pre><code>$ rubocop -R\n<\/code><\/pre>\n<p>or add the following directive to your <code>.rubocop.yml<\/code>:<\/p>\n<pre><code>AllCops:\n  RunRailsCops: true\n<\/code><\/pre>\n<h2>Configuration<\/h2>\n<p>The behavior of RuboCop can be controlled via the .rubocop.yml configuration file. It makes it possible to enable\/disable certain cops (checks) and to alter their behavior if they accept any parameters. The file can be placed either in your home directory or in some project directory.<\/p>\n<p>RuboCop will start looking for the configuration file in the directory where the inspected file is and continue its way up to the root directory.<\/p>\n<p>The file has the following format:<\/p>\n<pre><code>inherit_from: ..\/.rubocop.yml\n\nStyle\/Encoding:\n  Enabled: false\n\nMetrics\/LineLength:\n  Max: 99\n<\/code><\/pre>\n<p><strong>Note<\/strong>: Qualifying cop name with its type, e.g., <code>Style<\/code>, is recommended, but not necessary as long as the cop name is unique across all types.<\/p>\n<h3>Inheritance<\/h3>\n<p>The optional <code>inherit_from<\/code> directive is used to include configuration from one or more files. This makes it possible to have the common project settings in the <code>.rubocop.yml<\/code> file at the project root, and then only the deviations from those rules in the subdirectories. The files can be given with absolute paths or paths relative to the file where they are referenced. The settings after an <code>inherit_from<\/code> directive override any settings in the file(s) inherited from. When multiple files are included, the first file in the list has the lowest precedence and the last one has the highest. The format for multiple inheritance is:<\/p>\n<pre><code>inherit_from:\n  - ..\/.rubocop.yml\n  - ..\/conf\/.rubocop.yml\n<\/code><\/pre>\n<h3>Defaults<\/h3>\n<p>The file config\/default.yml under the RuboCop home directory contains the default settings that all configurations inherit from. Project and personal <code>.rubocop.yml<\/code> files need only make settings that are different from the default ones. If there is no <code>.rubocop.yml<\/code> file in the project or home directory, <code>config\/default.yml<\/code> will be used.<\/p>\n<h3>Including\/Excluding files<\/h3>\n<p>RuboCop checks all files found by a recursive search starting from the directory it is run in, or directories given as command line arguments. However, it only recognizes files ending with <code>.rb<\/code> or extensionless files with a <code>#!.*ruby<\/code> declaration as Ruby files. Hidden directories (i.e., directories whose names start with a dot) are not searched by default. If you\u2019d like it to check files that are not included by default, you\u2019ll need to pass them in on the command line, or to add entries for them under <code>AllCops<\/code>\/<code>Include<\/code>. Files and directories can also be ignored through <code>AllCops<\/code>\/<code>Exclude<\/code>.<\/p>\n<p>Here is an example that might be used for a Rails project:<\/p>\n<pre><code>AllCops:\n  Include:\n    - '**\/Rakefile'\n    - '**\/config.ru'\n  Exclude:\n    - 'db\/**\/*'\n    - 'config\/**\/*'\n    - 'script\/**\/*'\n    - !ruby\/regexp \/old_and_unused\\.rb$\/\n\n# other configuration\n# ...\n<\/code><\/pre>\n<p>Files and directories are specified relative to the <code>.rubocop.yml<\/code> file.<\/p>\n<p><strong>Note<\/strong>: Patterns that are just a file name, e.g. <code>Rakefile<\/code>, will match that file name in any directory, but this pattern style deprecated. The correct way to match the file in any directory, including the current, is <code>**\/Rakefile<\/code>.<\/p>\n<p><strong>Note<\/strong>: The pattern <code>config\/**<\/code> will match any file recursively under <code>config<\/code>, but this pattern style is deprecated and should be replaced by <code>config\/**\/*<\/code>.<\/p>\n<p><strong>Note<\/strong>: The <code>Include<\/code> and <code>Exclude<\/code> parameters are special. They are valid for the directory tree starting where they are defined. They are not shadowed by the setting of <code>Include<\/code> and <code>Exclude<\/code> in other <code>.rubocop.yml<\/code> files in subdirectories. This is different from all other parameters, who follow RuboCop\u2019s general principle that configuration for an inspected file is taken from the nearest <code>.rubocop.yml<\/code>, searching upwards.<\/p>\n<p>Cops can be run only on specific sets of files when that\u2019s needed (for instance you might want to run some Rails model checks only on files whose paths match <code>app\/models\/*.rb<\/code>). All cops support the <code>Include<\/code> param.<\/p>\n<pre><code>Rails\/DefaultScope:\n  Include:\n    - app\/models\/*.rb\n<\/code><\/pre>\n<p>Cops can also exclude only specific sets of files when that\u2019s needed (for instance you might want to run some cop only on a specific file). All cops support the <code>Exclude<\/code> param.<\/p>\n<pre><code>Rails\/DefaultScope:\n  Exclude:\n    - app\/models\/problematic.rb\n<\/code><\/pre>\n<h3>Generic configuration parameters<\/h3>\n<p>In addition to <code>Include<\/code> and <code>Exclude<\/code>, the following parameters are available for every cop.<\/p>\n<h4>Enabled<\/h4>\n<p>Specific cops can be disabled by setting <code>Enabled<\/code> to <code>false<\/code> for that specific cop.<\/p>\n<pre><code>Metrics\/LineLength:\n  Enabled: false\n<\/code><\/pre>\n<h4>Severity<\/h4>\n<p>Each cop has a default severity level based on which department it belongs to. The level is <code>warning<\/code> for <code>Lint<\/code> and <code>convention<\/code> for all the others. Cops can customize their severity level. Allowed params are <code>refactor<\/code>, <code>convention<\/code>, <code>warning<\/code>, <code>error<\/code> and <code>fatal<\/code>.<\/p>\n<p>There is one exception from the general rule above and that is <code>Lint\/Syntax<\/code>, a special cop that checks for syntax errors before the other cops are invoked. It can not be disabled and its severity (<code>fatal<\/code>) can not be changed in configuration.<\/p>\n<pre><code>Metrics\/CyclomaticComplexity:\n  Severity: warning\n<\/code><\/pre>\n<h4>AutoCorrect<\/h4>\n<p>Cops that support the <code>--auto-correct<\/code> option can have that support disabled. For example:<\/p>\n<pre><code>Style\/PerlBackrefs:\n  AutoCorrect: false\n<\/code><\/pre>\n<h3>Automatically Generated Configuration<\/h3>\n<p>If you have a code base with an overwhelming amount of offenses, it can be a good idea to use <code>rubocop --auto-gen-config<\/code> and add an <code>inherit_from: .rubocop_todo.yml<\/code> in your <code>.rubocop.yml<\/code>. The generated file <code>.rubocop_todo.yml<\/code> contains configuration to disable all cops that currently detect an offense in the code. Then you can start removing the entries in the generated file one by one as you work through all the offenses in the code.<\/p>\n<h2>Disabling Cops within Source Code<\/h2>\n<p>One or more individual cops can be disabled locally in a section of a file by adding a comment such as<\/p>\n<pre><code># rubocop:disable Metrics\/LineLength, Style\/StringLiterals\n[...]\n# rubocop:enable Metrics\/LineLength, Style\/StringLiterals\n<\/code><\/pre>\n<p>You can also disable <em>all<\/em> cops with<\/p>\n<pre><code># rubocop:disable all\n[...]\n# rubocop:enable all\n<\/code><\/pre>\n<p>One or more cops can be disabled on a single line with an end-of-line comment.<\/p>\n<pre><code>for x in (0..19) # rubocop:disable Style\/AvoidFor\n<\/code><\/pre>\n<h2>Formatters<\/h2>\n<p>You can change the output format of RuboCop by specifying formatters with the <code>-f\/--format<\/code> option. RuboCop ships with several built-in formatters, and also you can create your custom formatter.<\/p>\n<p>Additionally the output can be redirected to a file instead of <code>$stdout<\/code> with the <code>-o\/--out<\/code> option.<\/p>\n<p>Some of the built-in formatters produce <strong>machine-parsable<\/strong> output and they are considered public APIs. The rest of the formatters are for humans, so parsing their outputs is discouraged.<\/p>\n<p>You can enable multiple formatters at the same time by specifying <code>-f\/--format<\/code> multiple times. The <code>-o\/--out<\/code> option applies to the previously specified <code>-f\/--format<\/code>, or the default <code>progress<\/code> format if no <code>-f\/--format<\/code> is specified before the <code>-o\/--out<\/code> option.<\/p>\n<pre><code># Simple format to $stdout.\n$ rubocop --format simple\n\n# Progress (default) format to the file result.txt.\n$ rubocop --out result.txt\n\n# Both progress and offense count formats to $stdout.\n# The offense count formatter outputs only the final summary,\n# so you'll mostly see the outputs from the progress formatter,\n# and at the end the offense count summary will be outputted.\n$ rubocop --format progress --format offenses\n\n# Progress format to $stdout, and JSON format to the file rubocop.json.\n$ rubocop --format progress --format json --out rubocop.json\n#         ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~\n#                 |               |_______________|\n#              $stdout\n\n# Progress format to result.txt, and simple format to $stdout.\n$ rubocop --output result.txt --format simple\n#         ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~\n#                  |                 |\n#           default format        $stdout\n<\/code><\/pre>\n<p>You can also load custom formatters.<\/p>\n<h3>Progress Formatter (default)<\/h3>\n<p>The default <code>progress<\/code> formatter outputs a character for each inspected file, and at the end it displays all detected offenses in the <code>clang<\/code> format. A <code>.<\/code> represents a clean file, and each of the capital letters means the severest offense (convention, warning, error or fatal) found in a file.<\/p>\n<pre><code>$ rubocop\nInspecting 26 files\n..W.C....C..CWCW.C...WC.CC\n\nOffenses:\n\nlib\/foo.rb:6:5: C: Missing top-level class documentation comment.\n    class Foo\n    ^^^^^\n\n...\n\n26 files inspected, 46 offenses detected\n<\/code><\/pre>\n<h3>Clang Style Formatter<\/h3>\n<p>The <code>clang<\/code> formatter displays the offenses in a manner similar to <code>clang<\/code>:<\/p>\n<pre><code>$ rubocop test.rb\nInspecting 1 file\nW\n\nOffenses:\n\ntest.rb:1:5: C: Use snake_case for method names.\ndef badName\n    ^^^^^^^\ntest.rb:2:3: C: Use a guard clause instead of wrapping the code inside a conditional expression.\n  if something\n  ^^\ntest.rb:2:3: C: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &amp;&amp;\/||.\n  if something\n  ^^\ntest.rb:4:5: W: end at 4, 4 is not aligned with if at 2, 2\n    end\n    ^^^\n\n1 file inspected, 4 offenses detected\n<\/code><\/pre>\n<h3>Fuubar Style Formatter<\/h3>\n<p>The <code>fuubar<\/code> style formatter displays a progress bar and shows details of offenses in the <code>clang<\/code> format as soon as they are detected. This is inspired by the Fuubar formatter for RSpec.<\/p>\n<pre><code>$ rubocop --format fuubar\nlib\/foo.rb.rb:1:1: C: Use snake_case for methods and variables.\ndef badName\n    ^^^^^^^\nlib\/bar.rb:13:14: W: File.exists? is deprecated in favor of File.exist?.\n        File.exists?(path)\n             ^^^^^^^\n 22\/53 files |======== 43 ========&gt;                           |  ETA: 00:00:02\n<\/code><\/pre>\n<h3>Emacs Style Formatter<\/h3>\n<p><strong>Machine-parsable<\/strong><\/p>\n<p>The <code>emacs<\/code> formatter displays the offenses in a format suitable for consumption by <code>Emacs<\/code> (and possibly other tools).<\/p>\n<pre><code>$ rubocop --format emacs test.rb\n\/Users\/bozhidar\/projects\/test.rb:1:1: C: Use snake_case for methods and variables.\n\/Users\/bozhidar\/projects\/test.rb:2:3: C: Favor modifier if\/unless usage when you have a single-line body. Another good alternative is the usage of control flow &amp;&amp;\/||.\n\/Users\/bozhidar\/projects\/test.rb:4:5: W: end at 4, 4 is not aligned with if at 2, 2\n<\/code><\/pre>\n<h3>Simple Formatter<\/h3>\n<p>The name of the formatter says it all \ud83d\ude42<\/p>\n<pre><code>$ rubocop --format simple test.rb\n== test.rb ==\nC:  1:  5: Use snake_case for method names.\nC:  2:  3: Use a guard clause instead of wrapping the code inside a conditional expression.\nC:  2:  3: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &amp;&amp;\/||.\nW:  4:  5: end at 4, 4 is not aligned with if at 2, 2\n\n1 file inspected, 4 offenses detected\n<\/code><\/pre>\n<h3>File List Formatter<\/h3>\n<p><strong>Machine-parsable<\/strong><\/p>\n<p>Sometimes you might want to just open all files with offenses in your favorite editor. This formatter outputs just the names of the files with offenses in them and makes it possible to do something like:<\/p>\n<pre><code>$ rubocop --format files | xargs vim\n<\/code><\/pre>\n<h3>JSON Formatter<\/h3>\n<p><strong>Machine-parsable<\/strong><\/p>\n<p>You can get RuboCop\u2019s inspection result in JSON format by passing <code>--format json<\/code> option in command line. The JSON structure is like the following example:<\/p>\n<pre><code>{\n  \"metadata\": {\n    \"rubocop_version\": \"0.9.0\",\n    \"ruby_engine\": \"ruby\",\n    \"ruby_version\": \"2.0.0\",\n    \"ruby_patchlevel\": \"195\",\n    \"ruby_platform\": \"x86_64-darwin12.3.0\"\n  },\n  \"files\": [{\n      \"path\": \"lib\/foo.rb\",\n      \"offenses\": []\n    }, {\n      \"path\": \"lib\/bar.rb\",\n      \"offenses\": [{\n          \"severity\": \"convention\",\n          \"message\": \"Line is too long. [81\/80]\",\n          \"cop_name\": \"LineLength\",\n          \"corrected\": true,\n          \"location\": {\n            \"line\": 546,\n            \"column\": 80,\n            \"length\": 4\n          }\n        }, {\n          \"severity\": \"warning\",\n          \"message\": \"Unreachable code detected.\",\n          \"cop_name\": \"UnreachableCode\",\n          \"corrected\": false,\n          \"location\": {\n            \"line\": 15,\n            \"column\": 9,\n            \"length\": 10\n          }\n        }\n      ]\n    }\n  ],\n  \"summary\": {\n    \"offense_count\": 2,\n    \"target_file_count\": 2,\n    \"inspected_file_count\": 2\n  }\n}\n<\/code><\/pre>\n<h3>Offense Count Formatter<\/h3>\n<p>Sometimes when first applying RuboCop to a codebase, it\u2019s nice to be able to see where most of your style cleanup is going to be spent.<\/p>\n<p>With this in mind, you can use the offense count formatter to outline the offended cops and the number of offenses found for each by running:<\/p>\n<pre><code>$ rubocop --format offenses\n\n87   Documentation\n12   DotPosition\n8    AvoidGlobalVars\n7    EmptyLines\n6    AssignmentInCondition\n4    Blocks\n4    CommentAnnotation\n3    BlockAlignment\n1    IndentationWidth\n1    AvoidPerlBackrefs\n1    ColonMethodCall\n--\n134  Total\n<\/code><\/pre>\n<h3>HTML Formatter<\/h3>\n<p>Useful for CI environments. It will create an HTML report like this.<\/p>\n<pre><code>$ rubocop --format html -o rubocop.html\n<\/code><\/pre>\n<h2>Compatibility<\/h2>\n<p>RuboCop supports the following Ruby implementations:<\/p>\n<ul>\n<li>MRI 1.9.3<\/li>\n<li>MRI 2.0<\/li>\n<li>MRI 2.1<\/li>\n<li>MRI 2.2<\/li>\n<li>JRuby in 1.9 mode<\/li>\n<li>Rubinius 2.0+<\/li>\n<\/ul>\n<h2>Editor integration<\/h2>\n<h3>Emacs<\/h3>\n<p>rubocop.el is a simple Emacs interface for RuboCop. It allows you to run RuboCop inside Emacs and quickly jump between problems in your code.<\/p>\n<p>flycheck &gt; 0.9 also supports RuboCop and uses it by default when available.<\/p>\n<h3>Vim<\/h3>\n<p>The vim-rubocop plugin runs RuboCop and displays the results in Vim.<\/p>\n<p>There\u2019s also a RuboCop checker in syntastic.<\/p>\n<h3>Sublime Text<\/h3>\n<p>If you\u2019re a ST user you might find the Sublime RuboCop plugin useful.<\/p>\n<h3>Brackets<\/h3>\n<p>The brackets-rubocop extension displays RuboCop results in Brackets. It can be installed via the extension manager in Brackets.<\/p>\n<h3>TextMate2<\/h3>\n<p>The textmate2-rubocop bundle displays formatted RuboCop results in a new window. Installation instructions can be found here.<\/p>\n<h3>Atom<\/h3>\n<p>The atom-lint package runs RuboCop and highlights the offenses in Atom.<\/p>\n<p>You can also use the linter-rubocop plugin for Atom\u2019s linter.<\/p>\n<h3>LightTable<\/h3>\n<p>The lt-rubocop plugin provides LightTable integration.<\/p>\n<h3>RubyMine<\/h3>\n<p>The rubocop-for-rubymine plugin provides basic RuboCop integration for RubyMine\/IntelliJ IDEA.<\/p>\n<h3>Other Editors<\/h3>\n<p>Here\u2019s one great opportunity to contribute to RuboCop &#8211; implement RuboCop integration for your favorite editor.<\/p>\n<h2>Git pre-commit hook integration<\/h2>\n<p>overcommit is a fully configurable and extendable Git commit hook manager. To use RuboCop with overcommit, add the following to your <code>.overcommit.yml<\/code> file:<\/p>\n<pre><code>PreCommit:\n  RuboCop:\n    enabled: true\n<\/code><\/pre>\n<h2>Guard integration<\/h2>\n<p>If you\u2019re fond of Guard you might like guard-rubocop. It allows you to automatically check Ruby code style with RuboCop when files are modified.<\/p>\n<h2>Rake integration<\/h2>\n<p>To use RuboCop in your <code>Rakefile<\/code> add the following:<\/p>\n<pre><code>require 'rubocop\/rake_task'\n\nRuboCop::RakeTask.new\n<\/code><\/pre>\n<p>If you run <code>rake -T<\/code>, the following two RuboCop tasks should show up:<\/p>\n<pre><code>rake rubocop                                  # Run RuboCop\nrake rubocop:auto_correct                     # Auto-correct RuboCop offenses\n<\/code><\/pre>\n<p>The above will use default values<\/p>\n<pre><code>require 'rubocop\/rake_task'\n\ndesc 'Run RuboCop on the lib directory'\nRuboCop::RakeTask.new(:rubocop) do |task|\n  task.patterns = ['lib\/**\/*.rb']\n  # only show the files with failures\n  task.formatters = ['files']\n  # don't abort rake on failure\n  task.fail_on_error = false\nend\n<\/code><\/pre>\n<h2>Extensions<\/h2>\n<p>It\u2019s possible to extend RuboCop with custom cops and formatters.<\/p>\n<h3>Loading Extensions<\/h3>\n<p>Besides the <code>--require<\/code> command line option you can also specify ruby files that should be loaded with the optional <code>require<\/code> directive in the <code>.rubocop.yml<\/code> file:<\/p>\n<pre><code>require:\n - ..\/my\/custom\/file.rb\n - rubocop-extension\n<\/code><\/pre>\n<p>Note: The paths are directly passed to <code>Kernel.require<\/code>. If your extension file is not in <code>$LOAD_PATH<\/code>, you need to specify the path as relative path prefixed with <code>.\/<\/code> explicitly, or absolute path.<\/p>\n<h3>Custom Cops<\/h3>\n<p>You can configure the custom cops in your <code>.rubocop.yml<\/code> just like any other cop.<\/p>\n<h4>Known Custom Cops<\/h4>\n<ul>\n<li>rubocop-rspec &#8211; RSpec-specific analysis<\/li>\n<\/ul>\n<h3>Custom Formatters<\/h3>\n<p>You can customize RuboCop\u2019s output format with custom formatters.<\/p>\n<h4>Creating Custom Formatter<\/h4>\n<p>To implement a custom formatter, you need to subclass <code>RuboCop::Formatter::BaseFormatter<\/code> and override some methods, or implement all formatter API methods by duck typing.<\/p>\n<p>Please see the documents below for more formatter API details.<\/p>\n<h4>Using Custom Formatter in Command Line<\/h4>\n<p>You can tell RuboCop to use your custom formatter with a combination of <code>--format<\/code> and <code>--require<\/code> option. For example, when you have defined <code>MyCustomFormatter<\/code> in <code>.\/path\/to\/my_custom_formatter.rb<\/code>, you would type this command:<\/p>\n<pre><code>$ rubocop --require .\/path\/to\/my_custom_formatter --format MyCustomFormatter\n<\/code><\/pre>\n<h2>Team<\/h2>\n<p>Here\u2019s a list of RuboCop\u2019s core developers:<\/p>\n<ul>\n<li>Bozhidar Batsov<\/li>\n<li>Jonas Arvidsson<\/li>\n<li>Yuji Nakayama<\/li>\n<li>Evgeni Dzhelyov<\/li>\n<\/ul>\n<h2>Logo<\/h2>\n<p>RuboCop\u2019s logo was created by Dimiter Petrov. You can find the logo in various formats here.<\/p>\n<p>The logo is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.<\/p>\n<h2>Contributors<\/h2>\n<p>Here\u2019s a list of all the people who have contributed to the development of RuboCop.<\/p>\n<p>I\u2019m extremely grateful to each and every one of them!<\/p>\n<p>If you\u2019d like to contribute to RuboCop, please take the time to go through our short contribution guidelines.<\/p>\n<p>Converting more of the Ruby Style Guide into RuboCop cops is our top priority right now. Writing a new cop is a great way to dive into RuboCop!<\/p>\n<p>Of course, bug reports and suggestions for improvements are always welcome. GitHub pull requests are even better! \ud83d\ude42<\/p>\n<p>You can also support my work on RuboCop and all my other projects via gratipay.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.rawgit.com\/gratipay\/gratipay-badge\/2.1.3\/dist\/gratipay.png\" \/><\/p>\n<h2>Mailing List<\/h2>\n<p>If you\u2019re interested in everything regarding RuboCop\u2019s development, consider joining its Google Group.<\/p>\n<h2>Freenode<\/h2>\n<p>If you\u2019re into IRC you can visit the <code>#rubocop<\/code> channel on Freenode.<\/p>\n<h2>Changelog<\/h2>\n<p>RuboCop\u2019s changelog is available here.<\/p>\n<h2>Copyright<\/h2>\n<p>Copyright \u00a9 2012-2015 Bozhidar Batsov. See LICENSE.txt for further details.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Role models are important. \u2013 Officer Alex J. Murphy \/ RoboCop RuboCop is a Ruby static code analyzer. Out of the box it will enforce many of the guidelines outlined in the community Ruby Style Guide. Most aspects of its behavior can be tweaked via various configuration options. Apart from reporting problems in your code, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7560","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7560"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7560\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}