DevToolPal
EN

Ruby Formatter

Format Ruby code with Syntax Tree, running on the real Ruby interpreter compiled to WebAssembly.

Runs locally, never uploaded

Options

The first format downloads the Ruby interpreter (about 13 MB) and can take a few seconds; later runs are instant.

Input

0 chars · 0 B

Output

0 chars · 0 B

What this tool does

The Ruby formatter rewrites Ruby code with consistent indentation and spacing. Paste a script, a Rails model or a Rakefile and it comes back with two-space indentation, one statement per line, spaces around operators and inside braces, and long method chains wrapped to fit the line width.

It uses Syntax Tree running on the real Ruby interpreter compiled to WebAssembly. Because the parser is Ruby’s own, modern syntax such as endless methods, pattern matching and keyword arguments is understood exactly as Ruby understands it.

How to use it

  1. Paste Ruby code, or open a .rb file.
  2. Wait for the first result. The interpreter is downloaded once, in the background.
  3. Pick a Line width. Lines longer than this are broken at method calls, arguments or hash entries.
  4. Copy or download the formatted code.

If the code does not parse, the tool shows Ruby’s syntax error, such as syntax error, unexpected 'end', so you can find the missing piece.

Example

The sample is a small shopping cart module written without care for layout: a class body that is not indented, initialize written on one line with semicolons, hashes without spaces and a ternary with no spaces around >. After formatting, the module and class are indented, each statement in initialize gets its own line, hash literals become { name: name, price: price, qty: qty } and the ternary reads cart.total > 10 ? "big order" : "small order".

What it does not change

The formatter only changes layout. It does not rename variables, convert hash rockets to the new syntax, or switch between single and double quotes. Comments are preserved. If you need those style fixes, run RuboCop’s autocorrect in your project after formatting.

Tips

  • Use the same line width as your project’s .rubocop.yml (Layout/LineLength) so formatted code passes your linter.
  • Formatting is a quick way to spot a missing end: Ruby reports where it gave up, usually just after the block that was not closed.

FAQ

› Which formatter does this use?

Syntax Tree, the Ruby formatter behind the official Prettier Ruby plugin. It runs on CRuby, the standard Ruby interpreter, compiled to WebAssembly, so it parses your code exactly the way Ruby does.

› Why is the first run slow?

The page downloads the Ruby interpreter and the formatter, about 13 MB, the first time you format something, and starts them in a background worker so the page stays responsive. After that, formatting takes a fraction of a second.

› Does it follow RuboCop's style?

Mostly. Syntax Tree produces two-space indentation, spaces inside hash braces and around operators, and one blank line between methods, which matches the default RuboCop layout rules. It does not change quote style or rewrite code the way `rubocop -a` does.

› Is my code uploaded?

No. Everything runs in your browser and nothing is sent to a server.