Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement