Class: RubyFromExcel::ExcelMatrixCollection
- Inherits:
-
Object
- Object
- RubyFromExcel::ExcelMatrixCollection
- Includes:
- Enumerable
- Defined in:
- lib/formulae/run/excel_matrix.rb
Instance Attribute Summary collapse
-
#matrices ⇒ Object
Returns the value of attribute matrices.
-
#max_columns ⇒ Object
Returns the value of attribute max_columns.
-
#max_rows ⇒ Object
Returns the value of attribute max_rows.
Instance Method Summary collapse
- #coerce_to_equal_sizes ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(*args) ⇒ ExcelMatrixCollection
constructor
A new instance of ExcelMatrixCollection.
- #matrix_map ⇒ Object
- #to_enum ⇒ Object
Constructor Details
#initialize(*args) ⇒ ExcelMatrixCollection
Returns a new instance of ExcelMatrixCollection.
7 8 9 10 11 12 |
# File 'lib/formulae/run/excel_matrix.rb', line 7 def initialize(*args) self.matrices = args.map do |a| a.respond_to?(:to_excel_matrix) ? a.to_excel_matrix : ExcelMatrix.new(a) end coerce_to_equal_sizes end |
Instance Attribute Details
#matrices ⇒ Object
Returns the value of attribute matrices.
5 6 7 |
# File 'lib/formulae/run/excel_matrix.rb', line 5 def matrices @matrices end |
#max_columns ⇒ Object
Returns the value of attribute max_columns.
5 6 7 |
# File 'lib/formulae/run/excel_matrix.rb', line 5 def max_columns @max_columns end |
#max_rows ⇒ Object
Returns the value of attribute max_rows.
5 6 7 |
# File 'lib/formulae/run/excel_matrix.rb', line 5 def max_rows @max_rows end |
Instance Method Details
#coerce_to_equal_sizes ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/formulae/run/excel_matrix.rb', line 14 def coerce_to_equal_sizes self.max_rows = matrices.max_by(&:rows).rows self.max_columns = matrices.max_by(&:columns).columns matrices.each do |m| m.add_columns!(max_columns - m.columns) m.add_rows!(max_rows - m.rows) end end |
#each(&block) ⇒ Object
23 24 25 |
# File 'lib/formulae/run/excel_matrix.rb', line 23 def each &block block ? to_enum.each { |e| yield e } : to_enum end |
#matrix_map ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/formulae/run/excel_matrix.rb', line 37 def matrix_map results = Array.new(max_rows) { Array.new(max_columns) } 0.upto(max_rows-1) do |j| 0.upto(max_columns-1) do |i| results[j][i] = yield *matrices.map { |m| m.values[j][i] } end end em = ExcelMatrix.new(results) if em.rows == 1 && em.columns == 1 return em.values.first.first else return em end end |
#to_enum ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/formulae/run/excel_matrix.rb', line 27 def to_enum Enumerator.new do |yielder| 0.upto(max_rows-1) do |j| 0.upto(max_columns-1) do |i| yielder << matrices.map { |m| m.values[j][i] } end end end end |