Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/dedent.rb
Instance Method Summary collapse
Instance Method Details
#dedent ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/dedent.rb', line 2 def dedent lines = split "\n" return self if lines.empty? indents = lines.map do |line| line =~ /\S/ ? (line.start_with?(" ") ? line.match(/^ +/).offset(0)[1] : 0) : nil end indents.compact! if indents.empty? # No lines had any non-whitespace characters. return ([""] * lines.size).join "\n" end min_indent = indents.min return self if min_indent.zero? lines.map { |line| line =~ /\S/ ? line.gsub(/^ {#{min_indent}}/, "") : line }.join "\n" end |