Class: Tefil::ColumnFormer

Inherits:
TextFilterBase show all
Defined in:
lib/tefil/columnformer.rb

Instance Method Summary collapse

Methods inherited from TextFilterBase

#filter, #initialize

Constructor Details

This class inherits a constructor from Tefil::TextFilterBase

Instance Method Details

#form(matrix, io = $stdout, separator = " ", left = false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tefil/columnformer.rb', line 27

def form(matrix, io = $stdout, separator = " ", left = false)
  #Obtain max length for each column.
  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|
      method = :mb_rjust
      method = :mb_ljust if left
      new_items[index] = item.send(method, max_lengths[index])
    end
    io.puts new_items.join(separator).sub(/ +$/, "")
  end
end