Skip to content

Instantly share code, notes, and snippets.

@rbwendt
Created April 19, 2017 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbwendt/6f2f6e486755ad95b459983156a11f36 to your computer and use it in GitHub Desktop.
Save rbwendt/6f2f6e486755ad95b459983156a11f36 to your computer and use it in GitHub Desktop.
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