FormatterHQ

Ruby Formatter & Indenter

Re-indent Ruby code: classes, modules, def, if/elsif/else, case/when, begin/rescue and do…end blocks get consistent indentation. Heredocs are left intact.

Input RUBY
Output RUBY
Ready.

How to use the Ruby Formatter

  1. Paste your Ruby code into the Input editor, or use Sample, Upload file or Load URL to load a .rb file.
  2. Choose the Indent; the Ruby community standard is 2 spaces.
  3. Press Format to re-indent every line based on Ruby’s block keywords.
  4. Use Copy or Download above the output panel to take the re-indented Ruby with you.

What this Ruby formatter does

This Ruby formatter is a keyword-aware re-indenter. It reads your code line by line, tracks where blocks open and close (class, module, def, if, unless, case, while, begin, do, brackets) and where they end, and re-indents every line accordingly. It is the fix you need when code pasted from a chat, an email or a PDF has lost its indentation, or when tabs and spaces were mixed.

To be clear about its scope: it changes indentation only. Spacing inside a line, such as @items=items, stays as written, and it does not rewrite syntax the way RuboCop's autocorrect or the Standard gem do.

Example

module Billing
class Invoice
attr_reader :items
def initialize(items=[])
@items=items
end
def total
items.sum { |i| i[:price]*i[:qty] }
end
def status
case total
when 0 then :empty
else
:open
end
end
end
end
module Billing
  class Invoice
    attr_reader :items
    def initialize(items=[])
      @items=items
    end
    def total
      items.sum { |i| i[:price]*i[:qty] }
    end
    def status
      case total
      when 0 then :empty
      else
        :open
      end
    end
  end
end

Nested module and class bodies, methods, and the case expression are indented two spaces per level. when and else line up with case, which is the layout recommended by the community Ruby Style Guide.

Ruby constructs it understands

  • Block keywords: class, module, def, if, unless, while, until, for, case, begin and do, each closed by end.
  • Mid-block keywords: else, elsif, when, in, rescue and ensure are out-dented to the level of their opening keyword.
  • Modifier forms like return if empty? do not open a block, because the keyword is not at the start of the line.
  • Assigned conditionals like label = if paid then "Paid" else "Due" end spread over lines are indented correctly.
  • Endless methods (def full_name = "#{first} #{last}") do not open a block.
  • Brace blocks and literals: { |x| ... }, multi-line arrays and hashes indent by bracket depth.
  • Heredocs and =begin/=end comments are copied verbatim, since their contents are data.

Ruby style basics

The community Ruby Style Guide, which RuboCop enforces, sets conventions worth following alongside correct indentation:

  • Two spaces per level, never tabs.
  • snake_case for methods and variables, CamelCase for classes and modules, SCREAMING_SNAKE_CASE for constants.
  • Spaces around operators and after commas: @items = items, i[:price] * i[:qty].
  • Prefer { } for single-line blocks and do ... end for multi-line blocks.

For automatic enforcement of those rules in a project, run rubocop -a or standardrb --fix locally; this page gives you fast, dependency-free indentation in the browser.

Finding a missing end

Ruby's "unexpected end-of-input" error means some block was never closed. Paste the file here and look at the last line: if end is still indented instead of flush left, a block above it is missing its end. Scan upward for the first place where the indentation looks deeper than you intended. Too many end keywords show up the other way, as lines stuck at the left margin too early.

For Ruby data classes from JSON, try JSON to Ruby. Formatting ERB templates? The HTML parts are handled better by the HTML Formatter. All processing happens in your browser.

Frequently asked questions

Does this Ruby formatter work like RuboCop?
No. It re-indents lines based on Ruby’s block structure but does not change spacing inside lines or rewrite code. RuboCop and Standard enforce many more style rules.
Are heredocs changed?
No. Heredoc bodies and =begin/=end comment blocks are copied exactly as they are, because their contents are data rather than code.
Why is my last end not at the left margin?
The indenter counts one more block opening than closing, which means an end is missing somewhere above. Look for the first line whose indentation is deeper than expected.
Can I use 4 spaces or tabs?
Yes, choose them in Indent. The Ruby Style Guide recommends 2 spaces, which is the default.
Does it support Rails code?
Yes. Rails models, controllers, migrations and routes files are regular Ruby, and DSL blocks such as do ... end in routes are indented like any other block.

Last updated September 26, 2026

Related tools