class WhoAmI::TextTable

Public Class Methods

new(join:, prefix: "", suffix: "") click to toggle source
# File lib/who_am_i/text_table.rb, line 5
def initialize(join:, prefix: "", suffix: "")
  @join = join
  @prefix = prefix
  @suffix = suffix
  @rows = []
  @column_lengths = []
end

Public Instance Methods

each() { |output_row| ... } click to toggle source
# File lib/who_am_i/text_table.rb, line 28
def each
  if !block_given?
    return enum_for(:each)
  end

  @rows.each do |row|
    output_fields =
      row.map.with_index do |field, index|
        field.to_s.ljust(@column_lengths[index], " ")
      end

    output_row = @prefix + output_fields.join(@join) + @suffix

    yield output_row
  end
end
push(row) click to toggle source
# File lib/who_am_i/text_table.rb, line 13
def push(row)
  @rows.push(row)

  row.each.with_index do |field, index|
    @column_lengths[index] =
      if @column_lengths[index].nil?
        field.length
      else
        [@column_lengths[index], field.length].max
      end
  end

  true
end
to_s() click to toggle source
# File lib/who_am_i/text_table.rb, line 45
def to_s
  map(&:rstrip).join("\n") + "\n"
end