Ruby Code Formatter
Guide
Ruby Code Formatter
Paste your Ruby code and instantly reformat it to follow standard Ruby style: 2-space indentation, consistent operator spacing, trimmed trailing whitespace, and tidy block structure. The formatter runs entirely in your browser, so your code never leaves your machine.
How to Use
- Paste your Ruby source into the input area, or click Try an example to load a sample.
- Pick an indent size (2 spaces is the Ruby community default).
- Toggle Sort require / require_relative if you want the import block reordered alphabetically.
- Toggle Insert blank line between method definitions for cleaner class bodies.
- Click Format, then copy or download the result.
Features
- Standard Ruby style – 2-space indentation, no tabs, no trailing whitespace.
- Operator spacing – consistent spaces around
=,=>, comparison and compound assignment operators. - Block-aware indenting – recognizes
class,module,def,if,case,begin,do, and matchingend/}tokens. - Heredoc-safe –
<<~,<<-, and bare heredoc bodies are preserved verbatim so multi-line strings stay intact. - Modifier-form aware – trailing
if,unless,while, anduntilon a single line are not treated as block openers. - Optional require sorting – alphabetize a
require/require_relativeblock at the top of a file. - Optional method spacing – insert a blank line between adjacent method definitions for readability.
- Privacy-first – everything runs in your browser, no upload, no logging.
FAQ
-
Why is 2-space indentation considered Ruby style?
The Ruby community standardized on 2-space indentation early on through the Ruby Style Guide and the original RuboCop defaults. Two-space indents keep deeply nested blocks (closures inside iterators, nested classes, when arms inside case) horizontally compact, which complements Ruby’s preference for short methods and expressive block syntax. Most popular Ruby projects, including Rails, follow this convention.
-
What is the difference between do...end and curly-brace blocks?
Functionally,
do...endand{ ... }describe the same construct — a block passed to a method — but they bind differently because of operator precedence.do...endbinds loosely (soarr.each do |x| ... end.sizecalls.sizeon the result ofeach, not on the block’s return), while{ ... }binds tightly to the nearest method call. Style guides typically reserve braces for single-line blocks anddo...endfor multi-line blocks. -
How does Ruby’s modifier form work for if and unless?
Ruby lets you place a conditional after a statement, e.g.
return nil if value.nil?. This is called the modifier form. It is syntactically a single expression, not a block, so it does not require anend. Modifier form reads naturally for short guard clauses but is generally avoided for complex conditions, where a multi-lineifblock is clearer. -
What is a heredoc and why are squiggly heredocs preferred?
A heredoc is a multi-line string literal opened with
<<TAGand closed by a line containing onlyTAG. Plain<<TAGrequires the closing tag to start at column zero, which clashes with indented code.<<-TAGallows the closing tag to be indented, but keeps every line of the body verbatim.<<~TAG(the squiggly heredoc, added in Ruby 2.3) removes the smallest common leading indentation from every line of the body, so the literal can match the surrounding code indent without leaking spaces into the resulting string.
Install Our Extensions
Add IO tools to your favorite browser for instant access and faster searching
恵 Scoreboard Has Arrived!
Scoreboard is a fun way to keep track of your games, all data is stored in your browser. More features are coming soon!
Must-Try Tools
View All New Arrivals
View AllUpdate: Our latest tool was added on May 15, 2026
