class PrimeTable::TabularView
Attributes
prime_numbers[R]
defining the state attributes
table[R]
defining the state attributes
Public Class Methods
new(n: 10)
click to toggle source
initialize new object with default 10 prime numbers
# File lib/prime_table.rb, line 40 def initialize(n: 10) @table = [] @prime_numbers = GetPrimes.fetch_primes(n) end
Public Instance Methods
tableize()
click to toggle source
shows the product output in STDOUT in tabular form
# File lib/prime_table.rb, line 46 def tableize # format the header of the table # join all prime numbers with the calculated str_separator table_header = "%#{str_separator}s" % '#' # put '#' so to indicate the pointer to row and column conjunction @table << (table_header << @prime_numbers.collect {|s| "%#{str_separator+1}s" % s}.join('')) # get the multiplier value of the prime number with others @prime_numbers.map do |x| # push the value for the first column of the table str = "%#{str_separator}s" % x # extract the column values apart for subsequent columns @prime_numbers.collect{|w| str << "%#{str_separator+1}s" % (x * w) } # puts formatted string to table array @table << str end # displays the table on STDOUT @table end
Private Instance Methods
str_separator()
click to toggle source
calculate the number of spaces needed to indent the table column values
# File lib/prime_table.rb, line 71 def str_separator # assuming the last value to be the max in array and should have biggest square value # which further converted to string to extract the length ((@prime_numbers.last || 1)**2).to_s.length end