Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/mina/String.rb

Overview

# String

Instance Method Summary collapse

Instance Method Details

#dedentObject

### dedent Equally indents strings for writing to a file


8
9
10
11
12
13
14
15
16
17
# File 'lib/mina/String.rb', line 8

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
  min_indent = indents.compact.min
  return self if min_indent.zero?
  lines.map { |line| line =~ /\S/ ? line.gsub(/^ {#{min_indent}}/, "") : line }.join "\n"
end