Ruby Formatter and Beautifier
WM-98Paste Ruby and get each line re-indented by its def, class, if and do blocks and its brackets. Only whitespace at the start and end of lines changes.
Runs in your browser. Nothing is uploaded.
Ruby code
Re-indented Ruby
How to use it
- Paste your Ruby, or open a .rb file or drop it on the input box.
- Pick 2, 3, 4 or 8 spaces, or a tab. Two is the common Ruby choice.
- Press Re-indent.
- Copy the result or download it as a .rb file.
How depth is worked out
The tool counts keywords that open a block, such as def, class, module, if, unless, while, case, begin and do, together with brackets, and matches them with end and closing brackets. else, elsif, when, rescue and ensure sit at the level of their opener. Modifier forms such as a trailing if or unless do not open a block, and a one-line endless def is not counted.
What is left alone
Here-documents, =begin blocks, __END__, strings, regular expressions, symbols and character literals are not changed inside. Spacing within a line, quote style and line breaks all stay as typed. Long chained calls and aligned hash arguments are not aligned, and trailing spaces are removed.
Honest limits
Ruby syntax depends on context, so this is a careful heuristic, not a parser and not RuboCop. A rare construct can be misread, for example a regular expression that looks like a division. Unbalanced brackets stop the tool with a line number. Keyword mismatches are tolerated rather than reported.
Error messages and what to do
| Message | What it means | Fix |
|---|---|---|
The ( opened here is never closed (line 1). | A parenthesis opened on the line shown was never closed. | Add the missing ) or check the line for quote-like text the scanner misread. |
The [ opened here is never closed (line 1). | A square bracket opened on the line shown was never closed. | Add the missing ]. |
Questions people ask
How do I format Ruby code online?
Paste the code, choose an indent, and press Re-indent. Blocks and brackets decide each line's depth, and nothing else is changed. Paste the code, choose an indent and press Re-indent. Blocks and brackets decide each line's depth, and nothing else in the code is changed.
Is this the same as RuboCop?
No. RuboCop checks style rules and can rewrite code. This tool only fixes leading whitespace. No. RuboCop checks style rules and can rewrite code. This tool only fixes the leading whitespace of lines, so it never renames or reorders anything.
Does it handle heredocs?
Yes. Heredoc bodies and their terminators are left as typed, including the squiggly form with a dash or tilde. Yes. Heredoc bodies and their terminators are left as typed, including the squiggly form with a tilde, so their text is never re-indented.
Why is a modifier if not treated as a block?
A trailing if or unless on the same line as a statement does not need an end, so it does not add depth. A trailing if or unless on the same line as a statement does not need an end, so it does not add depth to the lines after it.
What if a line is misindented anyway?
Context-dependent syntax can fool the scanner. Check the line, and if it is a common pattern that fails, simplify it or fix that line by hand.
About this tool
This is a re-indenter, not RuboCop and not a Ruby parser. It changes only the whitespace at the start of each line, using def, class, module, if, do, begin, case and similar keywords together with brackets to decide the depth. It does not add spaces, change quotes or move code, and it leaves here-documents, =begin blocks, __END__ and strings alone. Because Ruby syntax is context dependent, an unusual modifier or a regex that looks like division can be misread, and long chained calls or aligned arguments are not aligned. Unbalanced brackets give an error with a line number.