Sha256: a85d6ed32abd9d0801905aa257f2ddda91743c6f8c6c2741cd162340e13f50a8

Contents?: true

Size: 752 Bytes

Versions: 1

Compression:

Stored size: 752 Bytes

Contents

#! /usr/bin/env ruby
# coding: utf-8

#
#
#
module Tefil::ColumnFormer
  def self.form(matrix, io = $stdout, separator = " ", left = false)
    #Obtain max length for each column.
    max_lengths = []
    matrix.each do |row|
      row.each_with_index do |item, index|
        item = item.to_s
        max_lengths[index] ||= 0
        size = item.size
        max_lengths[index] = size if max_lengths[index] < size
      end
    end

    #Output
    matrix.each do |row|
      new_items = []

      form_left = ""
      form_left = "-" if left

      row.each_with_index do |item, index|
        new_items[index] = sprintf("%#{form_left}#{max_lengths[index]}s", item)
      end
      io.puts new_items.join(separator).sub(/ +$/, "")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tefil-0.0.3 lib/tefil/columnformer.rb