class PutsUtils::PutsTable::Formatter
Attributes
table[R]
Public Class Methods
new(table)
click to toggle source
# File lib/puts_utils/puts_table/formatter.rb, line 8 def initialize(table) @table = table @sizes = {} end
Public Instance Methods
to_s()
click to toggle source
# File lib/puts_utils/puts_table/formatter.rb, line 13 def to_s table.rows.map do |values| render_row(values) end.join("\n") end
Private Instance Methods
render_row(values)
click to toggle source
# File lib/puts_utils/puts_table/formatter.rb, line 21 def render_row(values) (+'').tap do |result| values.each_with_index do |value, column| result << format("%-#{size_of_column(column)}s\t", value) end end end
size_of_column(index)
click to toggle source
# File lib/puts_utils/puts_table/formatter.rb, line 29 def size_of_column(index) key = index.to_s.to_sym return @sizes[key] if @sizes.key?(key) @sizes[key] = table.column(index) .map(&:to_s) .max_by(&:length) .length end