Skip to content

Instantly share code, notes, and snippets.

@troelskn
Last active July 11, 2019 08:08
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 troelskn/7a2fe439a0162e646c17cfe51e15db0d to your computer and use it in GitHub Desktop.
Save troelskn/7a2fe439a0162e646c17cfe51e15db0d to your computer and use it in GitHub Desktop.
# 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