Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/common/util.rb

Instance Method Summary collapse

Instance Method Details

#columnize(separator = ' ') ⇒ Object

Convert an Array of Array into a String, formatted as a table.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/common/util.rb', line 3

def columnize(separator = '  ')
  each { |row| row.is_a? Array or raise NoMethodError, 'Must be called on an Array of Array.' }
  result = ''
  l = []
  each { |row| row.each_with_index { |f, i| l[i] = [l[i] || 0, f.to_s.length].max } }
  each do |row|
    row.each_with_index { |f, i| result << "#{f.to_s.ljust l[i]}#{separator}" }
    result << "\n"
  end
  result
end