Remove Common Indent

Remove the common leading whitespace from all lines in a multi-line string, normalizing indentation.

Code

General
lines = s.split("\n")
min_indent = lines.reject(&:empty?).map { |l| l[/^\s*/].length }.min || 0
return lines.map { |l| l[min_indent..] || '' }.join("\n")

Parameters

Indented multi-line string

Server

More Ruby Snippets