class ListToColumns::ColumnMajor
Columnar list that is column major order; that is the order is top->bottom, then right->left.
Attributes
indent[R]
space[R]
width[R]
Public Class Methods
new(list = nil, options = nil)
click to toggle source
# File lib/list_to_columns/column_major.rb, line 9 def initialize(list = nil, options = nil) options ||= {} @indent = options[:indent] || 0 @width = options[:width] || 78 @space = options[:space] || 2 @strings = Array(list).map(&:to_s) return if @strings.empty? # Pad out list so it is rectangular. @strings[number_of_columns * number_of_rows - 1] ||= nil end
Public Instance Methods
longest_string_length()
click to toggle source
# File lib/list_to_columns/column_major.rb, line 21 def longest_string_length @longest_string_length ||= @strings.map(&:length).max || 0 end
matrix()
click to toggle source
# File lib/list_to_columns/column_major.rb, line 37 def matrix return [].freeze if @strings.empty? @matrix ||= @strings .each_slice(number_of_rows) .to_a .transpose .map(&:compact) .map(&:freeze) .freeze end
number_of_columns()
click to toggle source
# File lib/list_to_columns/column_major.rb, line 25 def number_of_columns return 0 if @strings.empty? @number_of_columns ||= (width + space) / (longest_string_length + space) end
number_of_rows()
click to toggle source
# File lib/list_to_columns/column_major.rb, line 31 def number_of_rows return 0 if @strings.empty? @number_of_rows ||= (@strings.length.to_f / number_of_columns).ceil end
to_s()
click to toggle source
# File lib/list_to_columns/column_major.rb, line 49 def to_s matrix .map { |r| "#{' ' * indent}#{r.map { |s| pad s }.join(' ' * space)}" } .map(&:rstrip) .join("\n") end
Private Instance Methods
pad(string)
click to toggle source
# File lib/list_to_columns/column_major.rb, line 58 def pad(string) format "%-#{longest_string_length}s", string end