Last active
July 11, 2019 08:08
-
-
Save troelskn/7a2fe439a0162e646c17cfe51e15db0d 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
# takes an array of hashes and prints as a table on the console | |
def table_print(rows) | |
table = [rows.first.keys.map(&:to_s)] + rows.map(&:values).map { |v| v.map(&:to_s) } | |
sizes = table.map { |row| row.map(&:size) }.transpose.map(&:max) | |
puts table.map { |row| row.each_with_index.map { |txt, idx| txt.rjust(sizes[idx]) }.join("\t") }.join("\n") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment