Split across newlines and return the fewest number of indentation characters found on each line
# File lib/indentation/string_mod.rb, line 25 def find_least_indentation(options = {:ignore_blank_lines => true, :ignore_empty_lines => true}) # Cannot ignore empty lines unless we're also ignoring blank lines options[:ignore_blank_lines] = options[:ignore_empty_lines] ? true : options[:ignore_blank_lines] empty? ? 0 : split("\n", -1).reject{|line| if options[:ignore_empty_lines] line.strip.empty? elsif options[:ignore_blank_lines] line.empty? else false end }.collect{|substr| substr.match(%r^[ \t]*/).to_s.length}.min end
Return an indented copy of this string Arguments:
num - How many of the specified indentation to use.
Default for spaces is 2. Default for other is 1. If set to a negative value, removes that many of the specified indentation character, tabs, or spaces from the beginning of the string
i_char - Character (or string) to use for indentation
# File lib/indentation/string_mod.rb, line 9 def indent(num = nil, i_char = ' ') _indent(num, i_char) end
Indents this string Arguments:
num - How many of the specified indentation to use.
Default for spaces is 2. Default for other is 1. If set to a negative value, removes that many of the specified indentation character, tabs, or spaces from the beginning of the string
i_char - Character (or string) to use for indentation
# File lib/indentation/string_mod.rb, line 20 def indent!(num = nil, i_char = ' ') replace(_indent(num, i_char)) end
Find the least indentation of all lines within this string and remove that amount (if any) Can pass an optional modifier that changes the indentation amount removed
# File lib/indentation/string_mod.rb, line 41 def reset_indentation(modifier = 0) indent(-find_least_indentation + modifier) end
Replaces the current string with one that has had its indentation reset Can pass an optional modifier that changes the indentation amount removed
# File lib/indentation/string_mod.rb, line 47 def reset_indentation!(modifier = 0) indent!(-find_least_indentation + modifier) end