Created
April 19, 2017 20:12
-
-
Save rbwendt/6f2f6e486755ad95b459983156a11f36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def to_table | |
column_sizes = self.reduce([]) do |lengths, row| | |
row.each_with_index.map{|iterand, index| [lengths[index] || 0, iterand.to_s.length].max} | |
end | |
puts head = '-' * (column_sizes.inject(&:+) + (3 * column_sizes.count) + 1) | |
self.each do |row| | |
row = row.fill(nil, row.size..(column_sizes.size - 1)) | |
row = row.each_with_index.map{|v, i| v = v.to_s + ' ' * (column_sizes[i] - v.to_s.length)} | |
puts '| ' + row.join(' | ') + ' |' | |
end | |
puts head | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment