Reason #102 • April 12th, 2026

ActiveSupport: String#squish

Ruby already gives us strip, lstrip and rstrip for trimming the edges of a string. ActiveSupport's squish goes one step further and normalizes the inside of strings as well.

Ruby
message = <<~TEXT
  Loving   Ruby
    is about

  enjoying the little things.
TEXT

message.squish
# => "Loving Ruby is about enjoying the little things."
      
JavaScript
const message = `
  Loving   Ruby
    is about

  enjoying the little things.
`;

message.trim().replace(/\s+/g, " ");
// => "Loving Ruby is about enjoying the little things."

This method is particularly useful for cleaning up text scraped from XML or HTML, or any other input where wonky whitespace is commonplace.

There is also a destructive version, squish!, which modifies the string in place.

History

String#squish was added to ActiveSupport in Rails 2.1.0, released in 2008.