Class: String

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

Instance Method Summary collapse

Instance Method Details

#drop_from_each_line(n) ⇒ Object

Drops n characters from the start of each line, but will not drop a line entirely; in other words, it will not drop a newline.



10
11
12
13
14
15
16
17
18
# File 'lib/unindentable.rb', line 10

def drop_from_each_line(n)
  self.lines.map do |line|
    k = 0
    line.chars.drop_while do |x|
      k += 1
      k <= n && x != "\n"
    end.join("")
  end.join("")
end

#find_minimum_indentObject

Returns the indent level (number of spaces at the start of a string) with multiple lines



22
23
24
# File 'lib/unindentable.rb', line 22

def find_minimum_indent
  self.lines.map { |s| s.index(/[^\s]/) unless s.empty? }.compact.min
end

#unindentObject

Removes common indentation from each line of a string



4
5
6
# File 'lib/unindentable.rb', line 4

def unindent
  drop_from_each_line(find_minimum_indent)
end