Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/indentation/array_mod.rb
Instance Method Summary collapse
-
#append_separator(*separators) ⇒ Object
Appends a given separator(s) to all but the last element in an array of Strings or if performed on an array of arrays, appends the separator element to the end of each array except the last.
-
#append_separator!(*separators) ⇒ Object
Append the given separator(s) to the current array.
-
#find_least_indentation(options = {:ignore_blank_lines => true, :ignore_empty_lines => true}) ⇒ Object
Get the least indentation of all elements.
-
#indent(num = nil, i_char = ' ') ⇒ Object
Return an indented array of strings (or an array of other arrays containing strings) Arguments: * num - How many of the specified indentation to use.
-
#indent!(num = nil, i_char = ' ') ⇒ Object
Apply indentation to this array Arguments: * num - How many of the specified indentation to use.
-
#reset_indentation(modifier = 0) ⇒ Object
Find the least indentation of all elements 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 array with one that has its indentation reset Can pass an optional modifier that changes the indentation amount removed.
Instance Method Details
#append_separator(*separators) ⇒ Object
Appends a given separator(s) to all but the last element in an array of Strings or if performed on an array of arrays, appends the separator element to the end of each array except the last
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/indentation/array_mod.rb', line 4 def append_separator(*separators) len = self.length ret_array = Array.new self.each_with_index do |element, i| new_element = element.dup unless i == len - 1 separators.each do |separator| new_element << separator end end ret_array << new_element end ret_array end |
#append_separator!(*separators) ⇒ Object
Append the given separator(s) to the current array
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/indentation/array_mod.rb', line 20 def append_separator!(*separators) len = self.length self.each_with_index do |element, i| unless i == len - 1 separators.each do |separator| element << separator end end end end |
#find_least_indentation(options = {:ignore_blank_lines => true, :ignore_empty_lines => true}) ⇒ Object
Get the least indentation of all elements
58 59 60 |
# File 'lib/indentation/array_mod.rb', line 58 def find_least_indentation( = {:ignore_blank_lines => true, :ignore_empty_lines => true}) self.collect{|array_element| array_element.find_least_indentation }.min end |
#indent(num = nil, i_char = ' ') ⇒ Object
Return an indented array of strings (or an array of other arrays containing strings) 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
38 39 40 41 42 |
# File 'lib/indentation/array_mod.rb', line 38 def indent(num = nil, i_char = ' ') self.collect do |array_element| array_element.indent(num, i_char) end end |
#indent!(num = nil, i_char = ' ') ⇒ Object
Apply indentation to this array 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
51 52 53 54 55 |
# File 'lib/indentation/array_mod.rb', line 51 def indent!(num = nil, i_char = ' ') self.collect! do |array_element| array_element.indent!(num, i_char) end end |
#reset_indentation(modifier = 0) ⇒ Object
Find the least indentation of all elements and remove that amount (if any) Can pass an optional modifier that changes the indentation amount removed
64 65 66 |
# File 'lib/indentation/array_mod.rb', line 64 def reset_indentation(modifier = 0) indent(-find_least_indentation + modifier) end |
#reset_indentation!(modifier = 0) ⇒ Object
Replaces the current array with one that has its indentation reset Can pass an optional modifier that changes the indentation amount removed
70 71 72 |
# File 'lib/indentation/array_mod.rb', line 70 def reset_indentation!(modifier = 0) indent!(-find_least_indentation + modifier) end |