Ruby Code Formatter & Beautifier
Format Ruby code with proper indentation and block alignment — all in your browser, nothing sent to any server
Formatted Ruby code will appear here.
A Rails controller with nested if/unless blocks, multi-line method chains, and inconsistent indentation between do...end blocks and brace blocks. You need to see the control flow before you can refactor it — but first the formatting needs to be consistent.
What Is a Ruby Formatter?
A Ruby formatter takes Ruby source code with inconsistent indentation or spacing and restructures it according to the Ruby community style guide. It normalizes indentation for method definitions, class bodies, blocks, and control structures. Whether you’re working with Rails controllers, Rake tasks, RSpec specs, or plain Ruby scripts, a formatter makes the code structure immediately readable.
This tool supports:
- 2-space indentation — the Ruby community standard
- Block style normalization — proper indentation for
do...endand{ }blocks - Method chain formatting — aligned multi-line chains (
.map.select.reduce) - Consistent spacing — around operators, after commas, inside hash rockets
Ruby Style Conventions
The Ruby community has strong formatting conventions, codified in the community style guide and enforced by RuboCop:
- 2-space indentation — never tabs, never 4 spaces
do...endfor multi-line blocks,{ }for single-line blocks- No parentheses for method definitions with no parameters:
def greetnotdef greet() - Snake_case for methods and variables, CamelCase for classes and modules
- Trailing comma in multi-line arrays and hashes for cleaner diffs
unlessfor single negative conditions:unless xrather thanif !x
Common Use Cases
Rails development: Format controllers, models, and views before code review. Consistent formatting across the MVC layers makes the application structure clearer.
RSpec test cleanup: Test files accumulate formatting inconsistencies as contexts and examples grow. Format them to clearly show the nesting of describe, context, and it blocks.
Gem development: Before publishing a gem, format all source files for a professional, consistent codebase that encourages contributions.
Learning Ruby: Properly formatted Ruby — with its readable, English-like syntax — is especially accessible to beginners when the indentation clearly shows block nesting.
Frequently Asked Questions
Does this formatter handle Ruby blocks and procs?
Yes. Both do...end blocks and brace blocks ({ |x| x + 1 }) are formatted with proper indentation. Lambda literals (->(x) { x + 1 }) and Proc.new are also handled.
Can it format ERB templates?
The formatter handles pure Ruby code. ERB templates (.erb files) mixing HTML and Ruby are not supported — use an ERB-aware formatter for those.
Does it handle heredocs?
Yes. Heredoc strings (<<~HEREDOC ... HEREDOC) are preserved as-is. The formatter does not modify the contents of heredocs, only the surrounding Ruby code structure.
Is my Ruby code sent to a server? No. All formatting runs entirely in your browser. No code is transmitted to any server. Your source code stays completely private on your device.