Reason #103 • April 13th, 2026

ActiveSupport: String#truncate

UI text often needs to fit into a narrow space: cards, table cells, notifications, teasers. ActiveSupport's String#truncate provides a wonderful solution to that:

Ruby
headline = "A long time ago in a galaxy far, far away"

# Truncate at exactly 32 characters, including the "..."
headline.truncate(32)
# => "A long time ago in a galaxy f..."

# Truncate at the last space before the 32-character limit
headline.truncate(32, separator: " ")
# => "A long time ago in a galaxy..."

# Customize the omission string
headline.truncate(32, separator: " ", omission: "... (continued)")
# => "A long time ago... (continued)"
      
JavaScript
const truncate = (text, limit, { separator, omission = "..." } = {}) => {
  if (text.length <= limit) return text;
  if (limit <= omission.length) return omission.slice(0, limit);

  let head = text.slice(0, limit - omission.length);

  if (separator) {
    const cut = head.lastIndexOf(separator);
    if (cut !== -1) head = head.slice(0, cut);
  }

  return head + omission;
};

const headline = "A long time ago in a galaxy far, far away";

truncate(headline, 32);
// "A long time ago in a galaxy f..."

truncate(headline, 32, { separator: " " });
// "A long time ago in a galaxy..."

truncate(headline, 32, { separator: " ", omission: "... (continued)" });
// "A long time ago... (continued)"
      

What more could one want from a truncation helper?

History

String#truncate was added in Rails 3.0.0, released in 2010.

Before that, the same logic existed as a view helper in ActionView's TextHelper#truncate.

Reason #104 ?