Skip to main content

Ruby Code Formatter

Format and beautify Ruby source code — re-indent by block structure (class/module/def/if/unless/case/while/until/do), normalize operator and comma spacing, and choose 2-space, 4-space, or tab indentation. Strings, comments and heredocs are left untouched. Runs entirely in your browser.

Input

Drop a file or browse
One file · text
Or

Formatting Options

Output

Formatted Code
 
Was this helpful?

Guides

Paste messy Ruby and get it back cleanly indented. The Ruby Code Formatter re-derives indentation from your code's block structure, tidies up some obvious spacing, and lets you pick 2-space (the Ruby community convention), 4-space, or tab indentation. Everything runs in your browser — your code is never uploaded to a server.

What it does

  • Re-indents by block depth. It tracks Ruby's block keywords — class, module, def, if, unless, while, until, for, begin, case, and do blocks — and their matching end, then rewrites each line's leading whitespace to a consistent width. Mid-block keywords (else, elsif, when, in, rescue, ensure) are pulled back to the level of the block they belong to, and their bodies indented beneath them. It also follows multi-line arrays, hashes, and argument lists by bracket depth ((, [, {).
  • Leaves strings, comments, and heredocs alone. Single-quoted, double-quoted (including #{…} interpolation), backtick, and %w[…]/%q{…}-style percent literals are treated as opaque — a def or end that lives inside a string or a # comment can never shift your indentation or get re-spaced. Heredoc bodies (<<~SQL, <<-EOT, <<'EOF', <<`CMD`) and =begin/=end block comments are preserved byte-for-byte, since their whitespace is part of the text.
  • Normalizes spacing (optional). Adds consistent spacing around unambiguous operators (==, !=, <=, >=, <=>, &&, ||, =>, =~, and compound assignment like +=) and after commas, and collapses stray double spaces in code.
  • Collapses blank lines (optional). Long runs of empty lines are reduced to a single blank line.

How to use it

  1. Paste your Ruby code into the input box, or upload a .rb file.
  2. Choose your indent style — 2 spaces, 4 spaces, or tabs.
  3. Toggle spacing normalization and blank-line collapsing to taste.
  4. Copy the formatted result or download it as formatted.rb.

The output updates as you type.

Important: this is a re-indenter, not RuboCop

This tool does structural reindentation — it lines your code up by block depth. It is not a full, syntax-validating formatter like RuboCop or Rufo, which parse the whole program with a real Ruby parser. Because those tools are written in Ruby and need a Ruby runtime, none of them can run in a browser, which is why this uses a lightweight, dependency-free heuristic instead.

Practically, that means it will not: wrap long lines, enforce a style guide, convert { … } blocks to do … end, rewrite hash syntax, or catch syntax errors. It also won't touch spacing around a bare =, because an assignment x = 1 and a keyword-argument default f(x=1) are indistinguishable without parsing the surrounding call.

Does it handle if used as a modifier?

Yes. A trailing if/unless/while/until (as in return nil if x.blank?) opens no block, so it doesn't affect indentation — only a leading block keyword does.

Is my code sent anywhere?

No. All formatting happens locally in your browser, so you can safely format proprietary or sensitive Ruby without it leaving your machine.

rubycode formatterbeautifyindentpretty printruby beautifier

Use it from code

From 3 credits per call

REST API

curl -X POST https://api.iotools.cloud/v1/tool/ruby-code-formatter \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "rubyInput": "class Report\ndef summarize(rows)\ntotal=0\nrows.ea…",
    "indentStyle": "2",
    "normalizeSpacing": "true",
    "collapseBlankLines": "true"
  }'

Swap in your own key from your account. The tool's fields are the body — no wrapper.

Ask an AI agent

Use the IOTools `ruby-code-formatter` tool (Ruby Code Formatter) on this input:

YOUR_INPUT_HERE

Paste this at any agent connected to the IOTools MCP server, then add your input.

Love the tools? Lose the ads.

One payment clears every ad from your account, for good. No subscription, no tracking.