Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/code_writer/string_mod.rb
Instance Method Summary collapse
-
#align_left ⇒ Object
—————————————————————————————-.
-
#find_least_indentation(options = {:ignore_blank_lines => true, :ignore_empty_lines => true}) ⇒ Object
—————————————————————————————- Split across newlines and return the fewest number of indentation characters found on each line —————————————————————————————-.
-
#indent(num = nil, i_char = ' ') ⇒ Object
—————————————————————————————- Return an indented copy of this string Arguments: * num - How many of the specified indentation to use.
-
#indent!(num = nil, i_char = ' ') ⇒ Object
—————————————————————————————- Indents this string Arguments: * num - How many of the specified indentation to use.
-
#prefix(prefix, prefix_1 = prefix, paragraph: false) ⇒ Object
—————————————————————————————- Adds a prefix to every line of the string.
-
#reset_indentation(modifier = 0) ⇒ Object
—————————————————————————————- 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 —————————————————————————————-.
-
#reset_indentation!(modifier = 0) ⇒ Object
—————————————————————————————- Replaces the current string with one that has had its indentation reset Can pass an optional modifier that changes the indentation amount removed —————————————————————————————-.
Instance Method Details
#align_left ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# 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}) ⇒ Object
Split across newlines and return the fewest number of indentation characters found on each line
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/code_writer/string_mod.rb', line 41 def find_least_indentation( = {:ignore_blank_lines => true, :ignore_empty_lines => true}) # Cannot ignore empty lines unless we're also ignoring blank lines [:ignore_blank_lines] = [:ignore_empty_lines] ? true : [:ignore_blank_lines] empty? ? 0 : split("\n", -1).reject{|line| if [:ignore_empty_lines] line.strip.empty? elsif [:ignore_blank_lines] line.empty? else false end }.collect{|substr| substr.match(/^[ \t]*/).to_s.length}.min end |
#indent(num = nil, i_char = ' ') ⇒ Object
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
18 19 20 |
# 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 = ' ') ⇒ Object
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
32 33 34 |
# 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) ⇒ Object
Adds a prefix to every line of the string. If prefix_1 is given, then the first line will have prefix_1 as its prefix.
94 95 96 97 98 99 100 101 |
# 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) ⇒ Object
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
60 61 62 |
# File 'lib/code_writer/string_mod.rb', line 60 def reset_indentation(modifier = 0) indent(-find_least_indentation + modifier) end |
#reset_indentation!(modifier = 0) ⇒ Object
Replaces the current string with one that has had its indentation reset Can pass an optional modifier that changes the indentation amount removed
69 70 71 |
# File 'lib/code_writer/string_mod.rb', line 69 def reset_indentation!(modifier = 0) indent!(-find_least_indentation + modifier) end |