class Primer::Table

Attributes

array[R]

Public Class Methods

new(array:) click to toggle source
# File lib/primer/table.rb, line 3
def initialize(array:)
  @array = array
end

Public Instance Methods

strings() click to toggle source
# File lib/primer/table.rb, line 7
def strings
  rows.map.with_index do |row, i|
    "#{columns[i]} | #{row.join(' ')}"
  end
end

Private Instance Methods

columns() click to toggle source
# File lib/primer/table.rb, line 31
def columns
  [' ' * max_string_size, '-' * max_string_size] + formatted_array
end
format() click to toggle source
# File lib/primer/table.rb, line 45
def format
  "%#{max_string_size}d"
end
formatted_array() click to toggle source
# File lib/primer/table.rb, line 35
def formatted_array
  array.map { |num| "#{format % num}" }
end
formatted_multiplications() click to toggle source
# File lib/primer/table.rb, line 39
def formatted_multiplications
  multiplications.map do |row|
    row.map { |num| "#{format % num}" }
  end
end
max_string_size() click to toggle source
# File lib/primer/table.rb, line 49
def max_string_size
  (array.max**2).to_s.size
end
multiplications() click to toggle source
# File lib/primer/table.rb, line 17
def multiplications
  array.map do |el|
    array.map { |e| e * el }
  end
end
rows() click to toggle source
# File lib/primer/table.rb, line 23
def rows
  [formatted_array, separator] + formatted_multiplications
end
separator() click to toggle source
# File lib/primer/table.rb, line 27
def separator
  Array.new(array.size, '-' * max_string_size)
end