class Columns
Represents column widths for aligned output formatting.
Calculates the maximum width of each column across all rows for proper text alignment.
Definitions
def initialize(rows)
Initialize column width calculator.
Signature
-
parameter
rows
Array(Array)
The rows to calculate column widths from.
Implementation
def initialize(rows)
@rows = rows
@widths = calculate_widths(rows)
end
attr :widths
The calculated column widths.
Signature
-
attribute
Array(Integer)
def calculate_widths(rows)
Calculate the maximum width for each column.
Signature
-
parameter
rows
Array(Array)
The rows to analyze.
-
returns
Array(Integer)
The maximum width of each column.
Implementation
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