How to use the Ruby Formatter
Format Ruby code in three steps:
1
Paste or upload Ruby code
Paste Ruby source code into the input panel or upload a .rb, .gemspec, or .rake file. Rails code, plain Ruby scripts, and RSpec tests are all accepted.
2
Choose indent size
Select 2 spaces (RuboCop and The Ruby Style Guide default) or 4 spaces if your project's .rubocop.yml specifies otherwise.
3
Format, copy, or download
Click 'Format Ruby' to apply RuboCop-style indentation and Ruby syntax highlighting. Copy the result or download as a .rb file.
When to use this tool
Use the Ruby formatter when Ruby code needs clean RuboCop-consistent formatting before sharing:
- →Formatting Ruby code from The Ruby Programming Language book, Stack Overflow, or GitHub
- →Cleaning up Rails controller, model, or service object code from AI assistants before code review
- →Preparing Ruby examples for blog posts, RubyGems documentation, or tutorial articles
- →Reformatting Ruby scripts that have mixed indentation from multiple contributors
- →Tidying RSpec or Minitest test files before pushing to a shared repository
- →Formatting Ruby coding challenge solutions for readable submission on HackerRank or Codewars
Frequently asked questions
Q:Why does Ruby use 2 spaces instead of 4 like most languages?
The Ruby Style Guide (authored by the RuboCop maintainers and widely adopted in the community) specifies 2-space indentation. This is partly historical — Matz (Ruby's creator) favoured 2 spaces, and this became the community consensus. Two-space indentation is enforced by RuboCop by default and followed in Rails, Sinatra, RSpec, and virtually all Ruby open-source projects.
Q:How does the formatter handle Ruby's end-based block structure?
Ruby uses end keywords to close blocks opened by def, class, module, if, unless, while, until, for, case, and do/begin. The formatter tracks these openers and their corresponding end keywords to manage indentation levels. It also correctly handles mid-block modifiers (else, elsif, rescue, ensure, when) that neither open nor close a block but dedent before printing.
Q:Does it handle do...end blocks and curly brace {} blocks for Ruby?
Yes — both do...end block syntax and single-line curly brace blocks are recognised. Multi-line do...end blocks are indented relative to their surrounding code. Single-line {|x| x.upcase } blocks are kept on one line. The formatter does not convert between the two styles — that is a RuboCop cop decision for your project.
Q:How are Ruby symbols and instance variables highlighted?
Ruby symbols (:name, :method_name) are highlighted in orange, making them distinct from string literals and variable names. Instance variables (@var), class variables (@@var), and global variables ($var) are highlighted in green with italic styling, following the conventional visual distinction for these scopes.
Q:Does it work with Ruby on Rails code — ActiveRecord, controllers, and views?
Yes — Rails code follows standard Ruby syntax. ActiveRecord model methods (validates, belongs_to, has_many, scope, before_action), controller methods (before_action, render, redirect_to), and ERB helper calls are all valid Ruby and are indented correctly.
Q:What is the difference between RuboCop and StandardRB?
RuboCop is the dominant Ruby linter/formatter with hundreds of configurable cops. StandardRB is an opinionated, zero-configuration Ruby style guide (similar to JavaScript's Standard Style) that wraps RuboCop with a fixed, non-negotiable set of rules. This formatter follows RuboCop's default style, which is also the basis for StandardRB. The core indentation rules are identical between the two.