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,beginanddo, each closed byend. - Mid-block keywords:
else,elsif,when,in,rescueandensureare 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" endspread 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/=endcomments 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_casefor methods and variables,CamelCasefor classes and modules,SCREAMING_SNAKE_CASEfor constants.- Spaces around operators and after commas:
@items = items,i[:price] * i[:qty]. - Prefer
{ }for single-line blocks anddo ... endfor 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.