Ruby Beautifier
Re-indent Ruby code by its block structure.
Ruby Beautifier – Re-indent Ruby Code
Ruby uses keywords instead of braces for most blocks, which makes broken indentation especially confusing: a missing end can hide anywhere. The Ruby beautifier re-indents your code based on its block structure, so you can see at a glance where each method, class, and block begins and ends.
What it understands
- Definitions –
class,module, anddef, includingprivate defandclass << self. Endless methods likedef total = items.sumdo not open a block. - Blocks –
do ... endwith or without block parameters, and{ ... }blocks. - Conditionals and loops –
if,unless,while,until,for,case, andbegin, including assignments likeresult = case value. - Middle keywords –
else,elsif,when,in,rescue, andensureare aligned with the block they belong to. - Modifiers –
return nil if name.nil?does not open a block. - Strings,
%w[]literals, regexes, comments,=begin/=endblocks, heredocs, and the__END__section are respected.
Options
Ruby's community style guide uses 2-space indentation, which is the default. You can switch to 4 spaces or tabs.
What it does not do
The beautifier changes indentation only: it trims trailing whitespace and collapses repeated blank lines, but it does not add spaces around operators or rewrite syntax. For full style enforcement — spacing, quotes, hash syntax — run RuboCop's autocorrect in your project. The benefit of a pure re-indenter is that it can never change what your code does.
Finding a missing end
If the last line of the output is still indented, a block was not closed. Scan upwards for the first line whose indentation looks wrong — that is where the missing end belongs.
Related tools
Format YAML files such as database.yml with the YAML Beautifier, JSON API responses with the JSON Beautifier, and ERB-free HTML with the HTML Beautifier.
Frequently Asked Questions
Is this the same as RuboCop?
No. RuboCop is a linter and full formatter. This tool only fixes indentation, which makes it safe for any Ruby file and useful for quickly reading messy code.
Does it handle heredocs?
Yes. Heredoc bodies (<<~SQL, <<-EOS, <<EOS) are copied unchanged, and indentation continues correctly after the terminator.
Can it help me find a missing end?
Yes. If the final lines are still indented after formatting, some block is not closed. The first line with unexpected indentation shows where to look.