Class: FundingPrimer::MultiplicationTable
- Inherits:
-
Object
- Object
- FundingPrimer::MultiplicationTable
- Defined in:
- lib/funding_primer/multiplication_table.rb
Overview
FundingPrimer::MultiplicationTable
is a 2-dimensional multiplication table of an enumerator of numbers.
It can be thought of as the Cartesian product of a vector with itself, plus some optional headings
Instance Method Summary collapse
-
#initialize(numbers) ⇒ MultiplicationTable
constructor
A new instance of MultiplicationTable.
-
#raw_array ⇒ Object
produces and caches a raw 2D array of pair-wise products from @numbers.
-
#with_headings ⇒ Object
produces an array with row and column headings.
Constructor Details
#initialize(numbers) ⇒ MultiplicationTable
Returns a new instance of MultiplicationTable.
7 8 9 |
# File 'lib/funding_primer/multiplication_table.rb', line 7 def initialize(numbers) @numbers = numbers.to_a end |
Instance Method Details
#raw_array ⇒ Object
produces and caches a raw 2D array of pair-wise products from @numbers
Example:
FundingPrimer::MultiplicationTable.new(1..3).raw_array
# => [[1,2,3],
# [2,4,6],
# [3,6,9]]
19 20 21 |
# File 'lib/funding_primer/multiplication_table.rb', line 19 def raw_array @raw_array ||= @numbers.product(@numbers).map {|pair| pair.inject :*}.each_slice(@numbers.length).to_a end |
#with_headings ⇒ Object
produces an array with row and column headings
Example:
FundingPrimer::MultiplicationTable.new(1..3).with_headings
# => [[nil,1,2,3],
# [1, 1,2,3],
# [2, 2,4,6],
[3, 3,6,9]]
32 33 34 35 36 |
# File 'lib/funding_primer/multiplication_table.rb', line 32 def with_headings column_headings = [nil].concat @numbers array_with_row_headings = @numbers.zip(raw_array).map &:flatten array_with_row_headings.unshift column_headings end |