class ProductTable::Output

Public Class Methods

new(number_list, product_table) click to toggle source
# File lib/product_table/output.rb, line 5
def initialize(number_list, product_table)
  @number_list   = number_list
  @product_table = product_table
end

Public Instance Methods

build_table() click to toggle source
# File lib/product_table/output.rb, line 14
def build_table
  @column_size = get_column_size + 2
  @base_format = "%#{@column_size}s"*@number_list.count

  format, output = build_header    ""    , []
  format, output = build_separator format, output
  format, output = build_rows      format, output

  output.flatten!
  sprintf format, *output
end
print_table() click to toggle source

Private Instance Methods

build_header(format="", output=[]) click to toggle source
# File lib/product_table/output.rb, line 34
def build_header(format="", output=[])
  format += "%#{@column_size}s | " + @base_format
  output << " "
  output << @number_list
  [format, output]
end
build_rows(format="", output=[]) click to toggle source
# File lib/product_table/output.rb, line 47
def build_rows(format="", output=[])
  @product_table.each_with_index do |row, index|
    row_num = @number_list[index]
    format += "\n%#{@column_size}s | " + @base_format
    output << row_num.to_s
    output << row
  end
  [format, output]
end
build_separator(format="", output=[]) click to toggle source
# File lib/product_table/output.rb, line 41
def build_separator(format="", output=[])
  format += "%s"
  output << "\n" + "-" * ((@column_size * @number_list.count) + 10)
  [format, output]
end
get_column_size() click to toggle source

find element with most characters

# File lib/product_table/output.rb, line 29
def get_column_size
  all_elements = (@number_list + @product_table).flatten
  all_elements.map(&:to_s).max { |element| element.length }.length
end