Class: Vernier::Output::Top::Table
- Inherits:
-
Object
- Object
- Vernier::Output::Top::Table
- Defined in:
- lib/vernier/output/top.rb
Instance Method Summary collapse
- #<<(row) ⇒ Object
- #format_row(row) ⇒ Object
-
#initialize(header, row_limit) {|_self| ... } ⇒ Table
constructor
A new instance of Table.
- #row_separator ⇒ Object
- #to_s ⇒ Object
- #widths ⇒ Object
Constructor Details
#initialize(header, row_limit) {|_self| ... } ⇒ Table
Returns a new instance of Table.
16 17 18 19 20 21 |
# File 'lib/vernier/output/top.rb', line 16 def initialize(header, row_limit) @header = header @rows = [] @row_limit = row_limit yield self end |
Instance Method Details
#<<(row) ⇒ Object
23 24 25 |
# File 'lib/vernier/output/top.rb', line 23 def <<(row) @rows << row end |
#format_row(row) ⇒ Object
50 51 52 |
# File 'lib/vernier/output/top.rb', line 50 def format_row(row) "|" + row.map.with_index { |str, i| " " + str.ljust(widths[i] + 1) }.join("|") + "|" end |
#row_separator ⇒ Object
46 47 48 |
# File 'lib/vernier/output/top.rb', line 46 def row_separator @row_separator = "+" + widths.map { |i| "-" * (i + 2) }.join("+") + "+" end |
#to_s ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/vernier/output/top.rb', line 27 def to_s ( [ row_separator, format_row(@header), row_separator ] + @rows.first(@row_limit).map do |row| format_row(row) end + [row_separator] ).join("\n") end |
#widths ⇒ Object
39 40 41 42 43 44 |
# File 'lib/vernier/output/top.rb', line 39 def widths @widths ||= (@rows + [@header]).transpose.map do |col| col.map(&:size).max end end |