Class: Pmt::MultiplicationTable

Inherits:
Object
  • Object
show all
Defined in:
lib/pmt/multiplication_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n = 10) ⇒ MultiplicationTable

Returns a new instance of MultiplicationTable.



8
9
10
# File 'lib/pmt/multiplication_table.rb', line 8

def initialize(n = 10)
  @number_of_primes = n
end

Instance Attribute Details

#number_of_primesObject (readonly)

Returns the value of attribute number_of_primes.



6
7
8
# File 'lib/pmt/multiplication_table.rb', line 6

def number_of_primes
  @number_of_primes
end

Instance Method Details

#multiplication_table_arrayObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pmt/multiplication_table.rb', line 21

def multiplication_table_array
  @multiplication_table_array ||=
    begin
      primes = Primes.first(number_of_primes)
      table = [[nil] + primes]

      primes.each do |prime|
        row = [prime]
        primes.each do |prime2|
          row << prime * prime2
        end
        table << row
      end

      table
    end
end

#to_sObject



12
13
14
15
16
17
18
19
# File 'lib/pmt/multiplication_table.rb', line 12

def to_s
  Terminal::Table.new do |t|
    multiplication_table_array.each_with_index do |row, index|
      t << row
      t << :separator unless index == number_of_primes
    end
  end.to_s
end