skip to content

What is Standard Ruby (the standard gem), how do you run it with standardrb or rake standard, and how does it differ from configuring RuboCop yourself?

level: juniorimportance: must knowfreq 55%

answer

  1. RuboCop with a fixed ruleset
  2. standardrb, or rake standard via standard/rake
  3. standardrb --fix for safe fixes
  4. no .rubocop.yml; .standard.yml holds options
  5. double quotes, no line length, no Metrics

basics

~20 s

Standard Ruby is a gem that runs RuboCop with a fixed, non-configurable ruleset. Run standardrb (or rake standard after requiring standard/rake), add --fix for safe corrections; it ignores .rubocop.yml and reads only options from .standard.yml.

solid answer

~40 s

The `standard` gem is a linter and formatter **built on RuboCop** that ships one ruleset you cannot change: it decides, for every RuboCop cop and the rubocop-performance cops, whether it is on and how. You add `gem "standard"` to the Gemfile and run `standardrb` (named so to avoid clashing with other `standard` commands), or `rake standard` after `require "standard/rake"` in the Rakefile. A clean run prints nothing and exits 0. `standardrb --fix` (or `rake standard:fix`) applies safe corrections. Standard does **not** read `.rubocop.yml`; `.standard.yml` holds only options such as ignored paths, the Ruby version and plugins. The ruleset picks, for example, double-quoted strings, no `Layout/LineLength`, no `Metrics` cops and no `Style/Documentation`. Adding rules is possible through plugins; changing Standard's own choices is not.

code

ruby · 8 lines
ruby
# Gemfile
group :development, :test do
  gem "standard", require: false
end

# Rakefile
require "standard/rake"
task default: %i[test standard]

go deeper

for a junior

Recall the gem name, the standardrb command, rake standard with require "standard/rake", and standardrb --fix for safe corrections.

for a middle

Explain that Standard is RuboCop with a fixed ruleset, ignores .rubocop.yml, keeps options in .standard.yml, and pins its RuboCop version.

for a senior

Weigh the trade: no per-cop tuning and a pinned RuboCop series in exchange for zero configuration and upgrades that move rules and RuboCop together.

for a principal

Decide per codebase whether an unconfigurable ruleset fits, knowing that leaving Standard later means writing and owning a .rubocop.yml.

## What the gem is **Standard Ruby** (the `standard` gem) is a Ruby linter and formatter that runs **RuboCop** under the hood with a configuration you do not write. Its authors describe it as an *unconfigurable configuration*: for every built-in RuboCop cop, and for the rubocop-performance cops, Standard has already decided whether it runs and with which options. You can add rules through plugins; you cannot change the ones Standard ships. Under the hood Standard: - sets `AllCops: DisabledByDefault: true`, so only cops its configuration names are enabled; - loads three built-in `lint_roller` plugins: `standard-base` (RuboCop's own cops), `standard-custom` (a few Standard-specific cops) and `standard-performance`; - replaces RuboCop's configuration with its own, so a project's `.rubocop.yml` is **not read**, and RuboCop's `--config` flag is dropped. ## Running it | Task | Command line | Rake | |---|---|---| | check | `standardrb` | `rake standard` | | check and apply safe fixes | `standardrb --fix` | `rake standard:fix` | | also apply unsafe fixes | `standardrb --fix-unsafely` | `rake standard:fix_unsafely` | - The Rake tasks exist once the `Rakefile` says `require "standard/rake"`. A task fails, and so fails the build, when the run exits non-zero. - Extra flags reach the Rake tasks through the `STANDARDOPTS` environment variable, for example `rake standard STANDARDOPTS="--format progress"`. - With no arguments Standard inspects the Ruby files under the current directory; you can pass files or directories. - A clean run prints nothing and exits 0. When offenses exist, it prints them under a `standard: Use Ruby Standard Style` header and suggests the fix command, such as `Run standardrb --fix to fix up to 12 problems`. Standard also forwards most RuboCop command-line flags, so `standardrb --format json` works. ## What the ruleset looks like A few choices are worth knowing because they differ from RuboCop's defaults: 1. `Style/StringLiterals` enforces **double quotes**. 2. `Layout/LineLength` is **off**. 3. Every `Metrics` cop (`MethodLength`, `AbcSize` and the rest) is **off**. 4. `Style/Documentation` and `Style/FrozenStringLiteralComment` are **off**. The emphasis is on correctness cops and on formatting that `--fix` can apply mechanically, rather than on size limits a person has to negotiate. ## Differences from running RuboCop directly | Aspect | Plain RuboCop | Standard | |---|---|---| | rules | defaults plus your `.rubocop.yml` | fixed, versioned with the gem | | config file | `.rubocop.yml` with any cop setting | `.standard.yml` with options only | | turning a cop off | `Enabled: false` | not possible; ignore files or use a directive | | RuboCop version | whatever you pin | pinned by Standard (1.56 depends on `rubocop ~> 1.88.0`) | | upgrades | you review new cops and defaults | a Standard release updates RuboCop and the ruleset together | The last two rows are easy to miss: Standard 1.56 locks RuboCop to the 1.88 series, so RuboCop features added later, such as the `disable-next` directive or `AllCops: FailLevel`, are not available through it. ## Upgrading Standard's rules change only when the gem does. A Standard release typically updates the RuboCop dependency and decides how each cop RuboCop added is configured, so an upgrade is one `bundle update standard` and a review of any new offenses it reports. Because the whole ruleset is versioned with the gem, `Gemfile.lock` pins the rules as well as the tools: two machines with the same lockfile get identical results. The flip side is that you cannot take a new RuboCop release on your own schedule; you wait for the Standard release that adopts it. ## Where it fits - A project that wants formatting and linting without a style debate installs Standard and runs `standardrb --fix` in the editor and `standardrb` in CI. - A project that needs to tune individual cops, enforce `Metrics` limits, or use RuboCop features newer than Standard's pin uses RuboCop directly.

  • A project has both .rubocop.yml and the standard gem. Which settings does standardrb use?
    Only Standard's own ruleset plus the options in `.standard.yml`. Standard replaces RuboCop's configuration with its own and drops RuboCop's `--config` flag, so `.rubocop.yml` is ignored. If the file exists for editor tooling that runs `rubocop`, the two tools can disagree about the same code.
  • Why can't you use RuboCop 1.91 features such as the disable-next directive through Standard 1.56?
    Standard 1.56 declares `rubocop ~> 1.88.0`, which allows only the 1.88 series. Bundler therefore resolves RuboCop 1.88.x, and features added in 1.90 and 1.91 are not present. A new Standard release is what moves the RuboCop version forward.

saying these in an interview costs you the question

  • Standard reads .rubocop.yml and applies its cop settings on top of Standard's rules.
  • You can turn off a Standard cop with Enabled: false in .standard.yml.
  • standardrb uses the latest RuboCop installed, whatever Standard's version.
  • Standard enforces single-quoted strings and an 80-character line limit.
  • rake standard works in any project without requiring standard/rake.