RuboCop Cop for Elegant Ruby Code

EO principles respected here DevOps By Rultor.com We recommend RubyMine

rake Gem Version Yard Docs License Test Coverage Hits-of-Code

This is a RuboCop plugin that enforces Elegant Objects principles in Ruby code. Default RuboCop configuration is too permissive, allowing coding practices that lead to unmaintainable code: comments that lie, verbose methods, cryptic variable names, and so on. This plugin configures existing RuboCop cops with stricter settings and adds custom cops for rules that RuboCop doesn't provide out of the box. The custom cops add the following restrictions:

  • Classes must live inside a module; top-level classes are forbidden.
  • A class nested in a module must use compact namespace syntax.
  • Method names must be single lowercase verbs.
  • Variable names must be single lowercase nouns.
  • Each indentation step must add exactly two spaces.
  • Comments are forbidden, except SPDX, magic, RuboCop directives, and class docblocks.
  • Empty lines inside block bodies are forbidden.
  • Empty lines inside method bodies are forbidden.
  • A method cannot return nil explicitly.
  • Local variables that are assigned once and read once must be inlined.
  • Brackets must be paired on the same line, or start/end their own line.

First, install it:

gem install rubocop-elegant

Then, format your .rubocop.yml like this:

AllCops:
  EnabledByDefault: true
  NewCops: enable
plugins:
  - rubocop-minitest
  - rubocop-performance
  - rubocop-rake
  - rubocop-elegant # must be the last one

The rubocop-elegant plugin not only provides its own cops, but also configures default ones the "right" way.

How to contribute

Read these guidelines. Make sure your build is green before you contribute your pull request. You will need to have Ruby 2.3+ and Bundler installed. Then:

bundle update
bundle exec rake

If it's clean and you don't see any error messages, submit your pull request.