Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.34 KB | None | 0 0
  1. # takes an array of hashes and prints as a table on the console
  2. def table_print(rows)
  3. table = [rows.first.keys.map(&:to_s)] + rows.map(&:values).map { |v| v.map(&:to_s) }
  4. sizes = table.map { |row| row.map(&:size) }.transpose.map(&:max)
  5. puts table.map { |row| row.each_with_index.map { |txt, idx| txt.rjust(sizes[idx]) }.join("\t") }.join("\n")
  6. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement