Ruby Formatter

Paste a Rails model, a Rake task or a one-liner from the console and get readable, consistently indented Ruby. A real Ruby interpreter runs the formatter inside your browser.

Input

Settings

History

Load from URL

Real Ruby, in the browser

Most online Ruby beautifiers re-indent lines with regular expressions, which breaks on heredocs, %w[] literals and blocks with do ... end. This page takes a different route: Syntax Tree on CRuby (WASM). The CRuby interpreter itself is compiled to WebAssembly, and Syntax Tree — the formatter written by Kevin Newton that also powers several editor integrations — parses your code with Ruby’s own parser before printing it again.

The practical result is that anything Ruby accepts can be formatted, including endless method definitions (def total = ...), pattern matching with case/in, numbered block parameters and keyword arguments with shorthand values. Typical changes:

  • statements joined with ; are split onto separate lines
  • def ... end and class bodies are indented two spaces per level
  • spaces are normalised inside { |x| ... } blocks, hashes and around operators
  • long method chains are broken with each call on its own line
  • single-quoted strings become double-quoted, Syntax Tree’s default

Width, indentation and shortcuts

Ruby has no extra options on this page. The line-width setting is passed to Syntax Tree as its print width (80 by default, which is also where the toolbar starts). Indentation is fixed at two spaces, the near-universal Ruby convention, so the toolbar’s indent control has no effect here — Syntax Tree does not offer tabs or four-space indentation.

Format with Ctrl/Cmd+Enter, copy the output with Ctrl/Cmd+Shift+C, or press Ctrl/Cmd+K to search for another formatter, such as YAML for a Rails database.yml. There is no minified Ruby, so Ctrl/Cmd+Shift+M is unavailable on this page.

What to expect from the output

Syntax Tree is closer to Prettier than to RuboCop. It does not enforce cops such as Style/FrozenStringLiteralComment, rename anything or reorder methods; it only decides layout and a handful of canonical spellings like quote style. If your project runs rubocop -a with single-quote preference, expect it to flip the quotes back.

Comments are preserved, including magic comments on the first lines. Heredoc bodies are printed exactly as written, since changing their indentation would change the string.

Long argument lists and hashes are broken one element per line once they exceed the print width, with the closing bracket on its own line. Short blocks stay as { |x| ... }; when a block body no longer fits, Syntax Tree switches it to do ... end, which is the conventional form for multi-line blocks. A parse error anywhere in the file means nothing is reformatted, and the message is Ruby’s own, so it reads exactly as it would in ruby -c.

ERB templates (.html.erb) are not plain Ruby and will fail to parse. For Rails configuration, use the YAML formatter; for a Gemfile or .gemspec, paste it here directly — both are ordinary Ruby.

Examples

ActiveRecord model

Association macros are split onto their own lines, and the endless method definition is kept as one line because it fits.

Input
class Order<ApplicationRecord
belongs_to :customer;has_many :items,dependent: :destroy
scope :paid,->{where(status: "paid")}
validates :total,numericality: {greater_than_or_equal_to: 0}
def total_with_tax(rate=0.08) = (total*(1+rate)).round(2)
end
Output
class Order < ApplicationRecord
  belongs_to :customer
  has_many :items, dependent: :destroy
  scope :paid, -> { where(status: "paid") }
  validates :total, numericality: { greater_than_or_equal_to: 0 }
  def total_with_tax(rate = 0.08) = (total * (1 + rate)).round(2)
end
Open this example in the tool

Enumerable chain and string interpolation

The chain breaks one call per line; inside the interpolation the single-quoted format string becomes double-quoted.

Input
report=orders.group_by{|o|o[:customer]}.transform_values{|os|os.sum{|o|o[:total]}}.sort_by{|_,v|-v}.first(5).to_h
puts report.map{|name,total|"#{name.ljust(20)} #{format('%.2f',total)}"}.join("\n")
Output
report =
  orders
  .group_by { |o| o[:customer] }
  .transform_values { |os| os.sum { |o| o[:total] } }
  .sort_by { |_, v| -v }
  .first(5)
  .to_h
puts report
  .map { |name, total| "#{name.ljust(20)} #{format("%.2f", total)}" }
  .join("\n")
Open this example in the tool

Service object at width 100

A wider print width lets the hash literal stay on one line while the method bodies are expanded.

Input
class OrderService
def initialize;@orders=Hash.new{|h,k|h[k]=[]};end
def add(id,sku:,qty:1,price:) @orders[id]<<{sku: sku,qty: qty,price: price} end
end
Output
class OrderService
  def initialize
    @orders = Hash.new { |h, k| h[k] = [] }
  end

  def add(id, sku:, qty: 1, price:)
    @orders[id] << { sku: sku, qty: qty, price: price }
  end
end
Open this example in the tool

Common errors and how to fix them

ErrorCauseFix
syntax error, unexpected end-of-input, expecting 'end'A def, class, if or do block is missing its closing end. Ruby only notices when the input runs out.Count the openers against the end keywords and add the missing one; the last block you edited is the usual suspect.
syntax error, unexpected 'end'There is one end too many, often after converting a do ... end block to braces.Remove the extra end.
unterminated string meets end of fileA string literal or heredoc was never closed.Close the quote, or make sure the heredoc terminator (for example SQL) is on a line of its own.
syntax error, unexpected end-of-input, expecting ']'An array literal is open at the end of the input.Add the closing bracket.

Frequently asked questions

Is this the same as running stree format?

Yes. The page runs Syntax Tree on a WebAssembly build of CRuby, so it produces what the stree command line produces with the same print width.

Why are my single quotes now double quotes?

Syntax Tree prefers double quotes unless the string contains a double quote. RuboCop configured for single quotes will disagree; pick one tool as the source of truth.

Can I change the indentation to four spaces?

No. Syntax Tree always indents with two spaces, which is the standard for Ruby.

Does it format ERB or Haml templates?

No. Only plain Ruby is supported. Gemfiles, Rakefiles and gemspecs work because they are ordinary Ruby files.

Related tools