class RMatrix::PrintTable

Attributes

cells[RW]
column_count[RW]
column_justifications[RW]
row_count[RW]
separators[RW]

Public Class Methods

new() click to toggle source
# File lib/rmatrix/printing/print_table.rb, line 4
def initialize
  self.row_count = self.column_count = 0
  self.cells = {}
  self.column_justifications = {}
  self.separators = Hash.new(', ')
end

Public Instance Methods

[](column, row) click to toggle source
# File lib/rmatrix/printing/print_table.rb, line 66
def [](column, row)
  self.cells[[column, row]]
end
[]=(column, row, value) click to toggle source
# File lib/rmatrix/printing/print_table.rb, line 70
def []=(column, row, value)
  build_column!(column)
  build_row!(row)
  self.cells[[column, row]] = value
end
build_column!(idx) click to toggle source
# File lib/rmatrix/printing/print_table.rb, line 76
def build_column!(idx)
  self.column_count = [self.column_count || 0, idx.succ].max
end
build_row!(idx) click to toggle source
# File lib/rmatrix/printing/print_table.rb, line 80
def build_row!(idx)
  self.row_count = [self.row_count || 0 , idx.succ].max
end
cell_repr(cell) click to toggle source
# File lib/rmatrix/printing/print_table.rb, line 54
def cell_repr(cell)
  case cell
  when nil then ''
  when Numeric then numeric_to_truncated_string(cell)
  else "#{cell}"
  end
end
column_justification(i) click to toggle source
# File lib/rmatrix/printing/print_table.rb, line 27
def column_justification(i)
  case self.column_justifications[i]
  when nil then :right
  when :right then :right
  when :left then :left
  else raise "Unexpected justification for column #{self.column_justifications[i] }"
  end
end
column_width(column) click to toggle source
# File lib/rmatrix/printing/print_table.rb, line 48
def column_width(column)
  self.row_count.times.reduce(0) do |agg, row|
    [agg, self.cell_repr(self.cells[[column, row]]).length].max
  end
end
column_widths() click to toggle source
# File lib/rmatrix/printing/print_table.rb, line 42
def column_widths
  column_count.times.map do |i|
    column_width(i)
  end
end
numeric_to_truncated_string(numeric) click to toggle source
# File lib/rmatrix/printing/print_table.rb, line 62
def numeric_to_truncated_string(numeric)
  numeric.to_s
end
set_column_separator(column, separator) click to toggle source
# File lib/rmatrix/printing/print_table.rb, line 36
def set_column_separator(column, separator)
  self.row_count.times do |row|
    self.separators[[column, row]] =  separator
  end
end
to_s() click to toggle source
# File lib/rmatrix/printing/print_table.rb, line 11
def to_s
  widths = self.column_widths
  self.row_count.times.map do |row|
    self.column_count.times.flat_map do |column|
      cell_text = cell_repr(self.cells[[column, row]])
      justification = column_justification(column)
      width = widths[column]
      contents = case justification
      when :left  then cell_text.ljust(width)
      when :right then cell_text.rjust(width)
      end
      [contents,self.separators[[column, row]]]
    end[0...-1].join
  end.join("\n")
end