Ruby Beautifier

Re-indent Ruby code by its block structure.

0 chars
0 words
0 lines
0 chars
0 words
0 lines

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, and def, including private def and class << self. Endless methods like def total = items.sum do not open a block.
  • Blocks – do ... end with or without block parameters, and { ... } blocks.
  • Conditionals and loops – if, unless, while, until, for, case, and begin, including assignments like result = case value.
  • Middle keywords – else, elsif, when, in, rescue, and ensure are aligned with the block they belong to.
  • Modifiers – return nil if name.nil? does not open a block.
  • Strings, %w[] literals, regexes, comments, =begin/=end blocks, 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.

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

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.

Yes. Heredoc bodies (<<~SQL, <<-EOS, <<EOS) are copied unchanged, and indentation continues correctly after the terminator.

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.