Class: Flucti::Utilities::Table
- Inherits:
-
Object
- Object
- Flucti::Utilities::Table
- Defined in:
- lib/flucti/utilities/table.rb
Instance Method Summary collapse
- #<<(row) ⇒ Object
-
#initialize(*headings) ⇒ Table
constructor
A new instance of Table.
- #to_s ⇒ Object
Constructor Details
#initialize(*headings) ⇒ Table
Returns a new instance of Table.
4 5 6 |
# File 'lib/flucti/utilities/table.rb', line 4 def initialize(*headings) @headings, @rows = headings, [] end |
Instance Method Details
#<<(row) ⇒ Object
8 9 10 11 |
# File 'lib/flucti/utilities/table.rb', line 8 def <<(row) @rows << row self end |
#to_s ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/flucti/utilities/table.rb', line 13 def to_s widths = (0...@headings.size).map { |col| ([@headings] + @rows).map { |row| row[col].to_s.length }.max } lines = [] lines << @headings.zip(widths).map { |heading, width| " %-*s " % [width, heading] }.join('|') lines << widths.map { |width| '-' * (width + 2) }.join('+') @rows.each do |row| lines << row.zip(widths).map { |cell, width| " %#{cell.to_s =~ /^\d+\b/ ? '' : '-'}*s " % [width, cell] }.join('|') end lines * "\n" end |