Ruby Formatter: Indent Ruby Code Using Keyword Blocks
Ruby's block structure is defined by keywords (def/end, class/end, do/end) rather than curly braces, making it unique among popular languages. This formatter applies keyword-aware indentation — dedenting before end, else, elsif, rescue, and ensure, and indenting after def, class, module, if, do, and similar block-opening keywords.
else, elsif, rescue, ensure dedent before output and re-indent after — they are both the end of one block and the start of another.
Ruby's Keyword-Based Block Structure
Unlike languages that use { } for all blocks, Ruby uses paired keywords: def/end for methods, class/end for classes, module/end for modules, if/end for conditionals, do/end for iterator blocks. The formatter recognizes all of these and adjusts indentation accordingly. Brace blocks { } are also supported for single-line and multi-line closures.
The 2-Space Convention
The Ruby Style Guide specifies 2-space indentation, unlike Java, PHP, and Kotlin (4 spaces). This reflects Ruby's tendency toward expressive, nested code — 2 spaces keeps deeply nested code manageable. Rails code, gems, and virtually all Ruby projects follow this convention.
RuboCop for Production Code
RuboCop is Ruby's standard linter and formatter, configured via a .rubocop.yml file. The 'rubocop --auto-correct' command (or '-A' for all auto-corrections) fixes indentation, spacing, and many other style issues automatically. Most Ruby CI pipelines include a RuboCop check step.
Practical Examples
Formatting a Ruby Model Class
Clean up a Rails model class that was written without indentation.
- 1.Paste the Ruby class (methods, callbacks, associations etc.)
- 2.The formatter applies 2-space indentation (Ruby standard)
- 3.Review method definitions, conditional logic, and blocks
- 4.Copy to your editor or run rubocop --auto-correct for final polish
Keywords That Control Indentation
- Open: def, class, module, if, elsif, else, unless, while, until, for, begin, do, case
- Close: end, else, elsif, rescue, ensure, when
- Brace blocks: { increases depth, } decreases
- 2-space default (Ruby community standard)
Good Use Cases
- Formatting AI-generated Ruby code
- Cleaning up Rails models, controllers, or concern files
- Reading a Ruby gem's source code
- Teaching Ruby block structure to developers from Java or JavaScript
Frequently Asked Questions
What indentation does the Ruby style guide recommend?
The Ruby Style Guide (rubocop-hq) recommends 2-space indentation. This is the dominant convention in Ruby and Rails projects, unlike most other languages that use 4 spaces.
How does do/end vs { } work in Ruby?
Both do/end and { } are used for blocks in Ruby. Conventionally, { } is used for single-line blocks and do/end for multi-line blocks. This formatter tracks both: { increases depth, } decreases it; do increases depth, end decreases it.
What are else, elsif, rescue, and ensure?
These keywords both close one block and open another. The formatter dedents before outputting them (closing the previous block) and then indents for the following lines (opening the next block). This is the standard Ruby formatting behavior.
What is RuboCop and how do I use it?
RuboCop is the standard Ruby linter and formatter. It enforces the Ruby Style Guide. Run 'gem install rubocop' and 'rubocop --auto-correct' to automatically fix style violations including indentation. Most Ruby editors (VS Code with ruby-lsp, RubyMine) integrate RuboCop.