class MiniPGM::Printer
Helper class for printing data in an easily readable ASCII format
Example output:
+-----------+-------------+-------------+-------------+-------------+ | Smoker | Smoker_0 | Smoker_0 | Smoker_1 | Smoker_1 | +-----------+-------------+-------------+-------------+-------------+ | Pollution | Pollution_0 | Pollution_1 | Pollution_0 | Pollution_1 | +-----------+-------------+-------------+-------------+-------------+ | Cancer_0 | 0.03 | 0.05 | 0.001 | 0.02 | +-----------+-------------+-------------+-------------+-------------+ | Cancer_1 | 0.97 | 0.95 | 0.999 | 0.98 | +-----------+-------------+-------------+-------------+-------------+
Public Class Methods
print(rows)
click to toggle source
# File lib/mini_pgm/printer.rb, line 21 def print(rows) # calculate column widths num_columns = rows[0].length column_widths = (0...num_columns).map { |col| max_width(rows, col) } # write table div = write_divider(column_widths) rows.flat_map { |row| [write_row(column_widths, row), div] } .unshift(div) .join("\n") end
Private Class Methods
max_width(rows, column)
click to toggle source
# File lib/mini_pgm/printer.rb, line 35 def max_width(rows, column) rows.map { |row| row[column].length }.max end
write_divider(column_widths)
click to toggle source
# File lib/mini_pgm/printer.rb, line 39 def write_divider(column_widths) "+-#{column_widths.map { |g| '-' * g }.join('-+-')}-+" end
write_row(column_widths, values)
click to toggle source
# File lib/mini_pgm/printer.rb, line 43 def write_row(column_widths, values) "| #{values.map.with_index { |value, i| value.ljust(column_widths[i]) }.join(' | ')} |" end