Ruby Formatter

Paste Ruby code and get back a reindented version based on its def/class/module/if/do...end block nesting. This is a lightweight structural reindenter, not a full syntax-aware formatter like RuboCop. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-24

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

Drifted indentation

Input

def greet(name)
if name
puts "Hello, #{name}"
end
end

Output

def greet(name)
  if name
    puts "Hello, #{name}"
  end
end

Each `end` dedents to match its opening keyword, and content between them is indented one level deeper.

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.

References

Frequently Asked Questions