Class: Samovar::Output::Columns
- Inherits:
-
Object
- Object
- Samovar::Output::Columns
- Defined in:
- lib/samovar/output/columns.rb
Overview
Represents column widths for aligned output formatting.
Calculates the maximum width of each column across all rows for proper text alignment.
Instance Attribute Summary collapse
-
#widths ⇒ Object
readonly
The calculated column widths.
Instance Method Summary collapse
-
#calculate_widths(rows) ⇒ Object
Calculate the maximum width for each column.
-
#initialize(rows) ⇒ Columns
constructor
Initialize column width calculator.
Constructor Details
#initialize(rows) ⇒ Columns
Initialize column width calculator.
21 22 23 24 |
# File 'lib/samovar/output/columns.rb', line 21 def initialize(rows) @rows = rows @widths = calculate_widths(rows) end |
Instance Attribute Details
#widths ⇒ Object (readonly)
The calculated column widths.
29 30 31 |
# File 'lib/samovar/output/columns.rb', line 29 def widths @widths end |
Instance Method Details
#calculate_widths(rows) ⇒ Object
Calculate the maximum width for each column.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/samovar/output/columns.rb', line 35 def calculate_widths(rows) widths = [] rows.each do |row| row.each.with_index do |column, index| (widths[index] ||= []) << column.size end end return widths.collect(&:max) end |