class String
Public Instance Methods
align_left()
click to toggle source
# File lib/code_writer/string_mod.rb, line 77 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 && indentation_level > 0 string end
find_least_indentation(options = {:ignore_blank_lines => true, :ignore_empty_lines => true})
click to toggle source
# File lib/code_writer/string_mod.rb, line 41 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(/^[ \t]*/).to_s.length}.min end
indent(num = nil, i_char = ' ')
click to toggle source
# File lib/code_writer/string_mod.rb, line 18 def indent(num = nil, i_char = ' ') _indent(num, i_char) end
indent!(num = nil, i_char = ' ')
click to toggle source
# File lib/code_writer/string_mod.rb, line 32 def indent!(num = nil, i_char = ' ') replace(_indent(num, i_char)) end
prefix(prefix, prefix_1 = prefix, paragraph: false)
click to toggle source
# File lib/code_writer/string_mod.rb, line 94 def prefix(prefix, prefix_1 = prefix, paragraph: false) split_type = (paragraph)? "\n\n" : "\n" split(split_type).map .with_index { |line, i| (i == 0)? prefix_1 + line : prefix + line }.join("\n") end
reset_indentation(modifier = 0)
click to toggle source
# File lib/code_writer/string_mod.rb, line 60 def reset_indentation(modifier = 0) indent(-find_least_indentation + modifier) end
reset_indentation!(modifier = 0)
click to toggle source
# File lib/code_writer/string_mod.rb, line 69 def reset_indentation!(modifier = 0) indent!(-find_least_indentation + modifier) end
Private Instance Methods
_indent(num = nil, i_char = ' ')
click to toggle source
# File lib/code_writer/string_mod.rb, line 113 def _indent(num = nil, i_char = ' ') # Define number of indentations to use number = num # Default number to 2 if spaces or 1 if other number ||= (i_char == ' ') ? 2 : 1 str_arr = [] case when number >= 0 split("\n", -1).collect{|line| (i_char * number) + line}.join("\n") else i_regexp = Regexp.new("^([ \t]|#{i_char})") split("\n", -1).collect do |line| ret_str = String.new(line) number.abs.times do match = ret_str.sub!(i_regexp, '') break unless match end ret_str end.join("\n") end end