J2justifier = {Left: :ljust, Right: :rjust, Center: :center} =begin Justify columns of textual tabular input where the record separator is the newline and the field separator is a 'dollar' character. justification can be Symbol; (:Left, :Right, or :Center). Return the justified output as a string =end def aligner(infile, justification = :Left) fieldsbyrow = infile.map {|line| line.strip.split('$')} # pad to same number of fields per record maxfields = fieldsbyrow.map(&:length).max fieldsbyrow.map! {|row| row + ['']*(maxfields - row.length)} # calculate max fieldwidth per column colwidths = fieldsbyrow.transpose.map {|column| column.map(&:length).max } # pad fields in columns to colwidth with spaces justifier = J2justifier[justification] fieldsbyrow.map {|row| row.zip(colwidths).map {|field, width| field.send(justifier, width) }.join(" ") }.join("\n") end require 'stringio' textinfile = <