Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/unindentable.rb
Instance Method Summary collapse
-
#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.
-
#find_minimum_indent ⇒ Object
Returns the indent level (number of spaces at the start of a string) with multiple lines.
-
#unindent ⇒ Object
Removes common indentation from each line of a string.
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_indent ⇒ Object
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 |
#unindent ⇒ Object
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 |