Ruby Development Style Guide
Ruby coding styles and conventions for DocOps Lab projects.
This guide outlines the Ruby coding styles and conventions used in DocOps Lab projects.
Automated Style Enforcement
DocOps Lab projects using the docopslab-dev gem automatically enforce Ruby style guidelines through:
- RuboCop
-
Automated code style checking and auto-fixing
- Git Hooks
-
Pre-commit advisory checks, pre-push quality gates
- CI/CD Integration
-
Automated linting in GitHub Actions workflows
To apply style fixes: bundle exec rake labdev:heal:ruby
See DocOps Lab Dev-tooling Usage for setup details.
Conventions
DocOps Lab largely follows Ruby’s community conventions, with some exceptions. Conventions are either reiterated or clarified here.
However, conventions are not exhaustively listed, and deviations are rarely pointed out as such.
Naming Conventions
-
Use
snake_casefor variable and method names. -
Use
CamelCasefor class and module names. -
Use
SCREAMING_SNAKE_CASEfor constants. -
Use descriptive names that convey the purpose of the variable, method, or class.
-
Avoid abbreviations unless they are widely understood.
-
Use verbs for method names to indicate actions.
-
Use nouns for class and module names to indicate entities.
Architectural Conventions
-
Use classes and class instance methods for objects that work like objects — they have state and do not act on other objects' state.
-
Use module methods acting on objects or carrying out general operations/utility functions.
-
Use Rake for internal (developer) CLI; use Thor for user-facing CLI
-
Gems may begin life as a module within another gem.
Path Conventions
-
Use
lib/for main application code.-
lib/<project_name>.rbfor the main file -
lib/<project_name>/for supporting files and modules -
lib/<project_name>/<module_name>/for submodules
-
-
Use
spec/for specifications and tests. -
Use
docs/or_docs/for documentation. -
Use
build/for pre-runtime artifacts. -
Use
_build/as default in applications that generate files at runtime, unless another path is more appropriate (ex:_site/in Jekyll-centric apps). -
Do NOT assume or insist upon perfect alignment with Ruby path conventions:
-
SomeModuleorSomeClassmay be sourced atlib/some_module.rborlib/some_class.rbinstead oflib/some/module.rborlib/some/class.rb. -
Some modules like
SchemaGraphyandAsciiDocare never broken up intoschema_graphyorascii_docnamespaces. -
Modules with multiple parallel sibling modules in a category like (
WriteOps,DraftOps) belong in paths likelib/ops/write.rbinstead oflib/write_ops.rborlib/write/ops.rb.
-
Syntax Conventions
-
Use 2 spaces for indentation.
-
Limit lines to 120 characters or so when possible.
-
Use parentheses for method calls with arguments, but omit them for methods without arguments.
-
Do not use parentheses in method definitions (
def method_name arg1, arg2). -
Use single quotes for strings that do not require interpolation or special symbols.
-
Use double quotes for strings that require interpolation or special symbols.
Commenting Conventions
See DocOps Lab Code Commenting Guidance for detailed commenting conventions.
RuboCop Customization
These rules (“cops”) can be overridden on a per-project basis in the .config/rubocop.yml file.
See RuboCop Configuration for docopslab-dev-managed RuboCop configuration.
|
Project Ruby Style Guide (Customizations Only)
This document lists only deviations from the standard RuboCop defaults. For everything else, consult: RuboCop Style Guide (All Cops)
Generated from gems/docopslab-dev/assets/config-packs/rubocop/base.yml compared to built-in defaults.
All Cops
- Display Style Guide
-
true - New Cops
-
enable - Target Ruby Version
-
3.2
Layout: Space Around Equals In Parameter Default
- Enabled
-
false - Enforced Style
-
no_space
Metrics: Abc Size
- Enabled
-
false
Metrics: Block Length
- Allowed Methods
-
-
describe -
context -
feature -
scenario -
let -
let! -
subject -
task -
namespace
-
- Max
-
50
Metrics: Cyclomatic Complexity
- Enabled
-
false
Metrics: Perceived Complexity
- Enabled
-
false
Style: Method Call With Args Parentheses
- Allow Parentheses In Chaining
-
true - Allow Parentheses In Multiline Call
-
true
RuboCop Configuration
Ruby code style and quality checking.
- Base config
-
.config/.vendor/docopslab/rubocop.yml - Project config
-
.config/rubocop.yml(inherits viainherit_from) - Sync command
-
bundle exec rake labdev:sync:configs
The base configuration provides DocOps Lab Ruby style standards. Your project config can override any rule while maintaining consistency with the broader ecosystem.