Class: Tefil::ColumnFormer
- Inherits:
-
TextFilterBase
- Object
- TextFilterBase
- Tefil::ColumnFormer
- Defined in:
- lib/tefil/columnformer.rb
Instance Method Summary collapse
-
#form(matrix, io = $stdout, indent = 0) ⇒ Object
def form(matrix, io = $stdout, separator = “ ”, left = false).
-
#initialize(options = {}) ⇒ ColumnFormer
constructor
A new instance of ColumnFormer.
Methods inherited from TextFilterBase
Constructor Details
#initialize(options = {}) ⇒ ColumnFormer
Returns a new instance of ColumnFormer.
29 30 31 32 33 34 |
# File 'lib/tefil/columnformer.rb', line 29 def initialize( = {}) @just = [:just] || :left @separator = [:separator] || ' ' @transpose = [:transpose] super() end |
Instance Method Details
#form(matrix, io = $stdout, indent = 0) ⇒ Object
def form(matrix, io = $stdout, separator = “ ”, left = false)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tefil/columnformer.rb', line 37 def form(matrix, io = $stdout, indent = 0) #Obtain max length for each column. matrix = matrix.transpose if @transpose max_lengths = [] matrix.each do |row| row.each_with_index do |item, index| item = item.to_s max_lengths[index] ||= 0 size = print_size(item) max_lengths[index] = size if max_lengths[index] < size end end #Output matrix.each do |row| new_items = [] row.each_with_index do |item, index| item = item.to_s method = (@just.to_s + "_just").to_sym new_items[index] = item.send(method, max_lengths[index]) end io.print(" " * indent) io.puts new_items.join(@separator).sub(/ +$/, "") end end |