Method: String#align_left

Defined in:
lib/libis/tools/extend/string.rb

#align_leftObject

Align a multi-line string to the left by removing as much spaces from the left as possible.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/libis/tools/extend/string.rb', line 73

def align_left
  string = dup
  relevant_lines = string.split(/\r\n|\r|\n/).select { |line| line.size > 0 }
  indentation_levels = relevant_lines.map do |line|
    match = line.match(/^( +)[^ ]+/)
    match ? match[1].size : 0
  end
  indentation_level = indentation_levels.min
  string.gsub! /^#{' ' * indentation_level}/, '' if indentation_level > 0
  string
end