Overview
Introduction
Ruby code pasted from different sources often has inconsistent indentation around its def/class/if/do...end blocks.
This tool reindents each line based on that block structure, entirely in your browser, without requiring a Ruby interpreter.
What Is Ruby Formatter?
A lightweight reindenter that tracks nesting depth by matching leading keywords (def, class, module, if, unless, while, case, begin, do) against their closing end.
It's a whitespace-only pass: it never rewrites your code's tokens, only the leading indentation of each line.
How Ruby Formatter Works
Each line is checked against a small set of keyword patterns to decide whether it opens a new block, closes one, or is a mid-block keyword like else or rescue.
Depth increases on an opener and decreases on end, and each line is re-indented to match its current depth.
When To Use Ruby Formatter
Use it when a Ruby snippet's indentation has drifted after manual edits or a copy-paste.
It's a quick visual check that a block structure's def/end pairs look balanced.
Often used alongside PHP Formatter and Python Formatter.
Features
Advantages
- Runs entirely client-side with no Ruby runtime required.
- Handles the common def/class/if/do/end patterns most Ruby code uses.
- Recognizes mid-block keywords like else, elsif, when, rescue, and ensure, dedenting them correctly relative to their surrounding block.
Limitations
- Not a real Ruby parser: it won't understand modifier-form conditionals (`x if y`), heredocs, or multi-line string literals containing keyword-like text.
- For rigorous formatting, use a real tool like RuboCop in your local Ruby environment.
Examples
Best Practices & Notes
Best Practices
- Use this for a quick cleanup pass, then run a real Ruby linter locally before committing.
- Check for heredocs or multi-line strings containing keyword-like text before trusting the result blindly.
Developer Notes
Implemented as a single-pass line scanner over regex-matched opening/closing keywords; it does not tokenize strings or comments, so keyword-like text inside either can affect depth.
Ruby Formatter Use Cases
- Quickly cleaning up indentation in a pasted Ruby snippet
- Spotting an unbalanced def/end pair by eye
Common Mistakes
- Relying on this for production formatting instead of a real Ruby-aware tool like RuboCop.
- Assuming `{ }` block syntax is tracked the same as `do...end`; it isn't, only `do...end` blocks affect nesting depth.
Tips
- If the output looks wrong, check for a heredoc or multi-line string containing a keyword like `if` or `end` as plain text.
- Diff the reindented output against your original before committing it, so you can see exactly which lines shifted.