Skip to main content

Ruby Formatter Beautifier

Format and beautify raw or minified Ruby code with configurable indentation depth, blank-line handling, and operator spacing applied in a single pass. A tokenizer isolates string literals, heredocs, percent-literals, regex patterns, and comments so formatting changes never alter program output or break quoted content.

Runs entirely in your browser

This tool sends nothing over the network. Everything you enter is processed on your device and never reaches our servers.

Text
Productivity
Loading the tool
Reference

Documentation

Formatting Ruby code means tokenizing raw or minified source first, separating string literals, heredocs, percent-literal strings, regex patterns, and comments from executable code, then applying indentation and spacing rules only to the code tokens, so quoted content and comment text pass through unchanged. Heredocs (multi-line string literals opened with <<~ or <<-) and percent-literals (bracketed forms such as %w[] for word arrays or %i[] for symbol arrays) are common sources of false positives for a formatter that does not tokenize first, since their contents can resemble code but must never be reindented or have operator spacing applied inside them.

Indentation can use spaces or tabs, spaces by default, at a size from 1 to 8 characters, with 2 as the default matching Ruby community convention. Blank lines can be preserved exactly as they appear in the source, collapsed so consecutive blank lines become a single blank line, or removed entirely.

Removing trailing whitespace, on by default, strips spaces and tabs from the end of every line. Adding spaces around operators inserts a single space on both sides of assignment, comparison, arithmetic, and logical operators; an operator that appears inside a string, comment, or regex pattern is never touched, since it is not executable code.

Indentation increases after a block-opening keyword: def, class, module, if, unless, case, while, until, for, begin, or do. It decreases at end and at mid-block keywords that continue an existing block rather than opening a new one: elsif, else, when, rescue, and ensure. Formatting runs automatically after a 500-millisecond debounce as code is typed, so the effect of a setting change is visible immediately.

Consistent formatting improves readability, reduces merge conflicts, and enforces team style guides across Ruby projects of any size. The following scenarios illustrate where this formatter fits into common development workflows.

  • Code review preparation: Format a pull request branch before review so reviewers focus on logic changes rather than whitespace differences. Collapsing blank lines and normalizing indentation to 2 spaces eliminates style noise from diffs.
  • Legacy code cleanup: Import older Ruby files that mix tabs and spaces or use inconsistent indentation depths. Run the formatter once to bring them in line with current project standards without altering string content or comments.
  • Learning and teaching: Students working through Ruby tutorials can compare example code against its properly structured output. Instructors can demonstrate how block nesting translates to indentation levels by changing indent sizes.
  • Minified script expansion: Expand single-line or compressed Ruby scripts into readable multi-line form. One-liner blocks written with braces expand into indented do...end style layouts when the formatter encounters nested constructs.
  • Clipboard and snippet formatting: Copy Ruby code from Stack Overflow answers, blog posts, or documentation where indentation may have been stripped during publishing, then format it back into readable shape.
  • Configuration file consistency: Format Rake tasks, Gemfile blocks, and initializer scripts that accumulate ad-hoc edits over time. Normalizing operator spacing and blank-line handling keeps configuration files readable alongside application code.
  • Cross-team standardization: Teams merging codebases from different groups can run each file through the formatter with agreed-upon settings to unify style before combining repositories.
  • Quick prototyping: Draft Ruby logic in a compressed style for speed, then format the result before committing. The live debounce preview shows the formatted shape as each line is typed, catching nesting errors early.
Inputs, outputs, and what the Ruby Formatter Beautifier computes

What the Ruby Formatter Beautifier asks for and what it returns, as a plain list. Defaults, units, and ranges are the ones the form loads with.

Inputs

  • Ruby Code
  • Indentation Style · default: Spaces
  • Indent Size · default: 2
  • Blank Lines · default: Collapse to one
  • Remove trailing whitespace · default: on
  • Add spaces around operators · default: on
  • Formatted Output

Controls

Reset · Copy