Advertisement
Guest User

Untitled

a guest
May 29th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. def get_columns list
  2. list.map{|data| data.keys}.flatten.uniq
  3. end
  4.  
  5. def print_line items
  6. @lines_to_print << items
  7. end
  8.  
  9. def print_columns columns
  10. print_line columns.map(&:to_s)
  11. print_line ["_"]* columns.count
  12. end
  13.  
  14. def print_data data, columns
  15. print_line columns.map{|col| data[col].to_s}
  16. end
  17.  
  18. def print_all
  19. max_width = []
  20. @lines_to_print.each do |line|
  21. line.each_with_index do |col_val, index|
  22. if (max_width[index]||0)< col_val.size
  23. max_width[index] = col_val.size
  24. end
  25. end
  26. end
  27.  
  28. @lines_to_print.each do |line|
  29. line.each_with_index do |item, index|
  30. print item.rjust(max_width[index])
  31. print "|"
  32. end
  33. print "\n"
  34. end
  35. end
  36.  
  37. def table list
  38. @lines_to_print = []
  39. list = Array(list).map(&:to_hash)
  40. columns = get_columns list
  41.  
  42. print_columns columns
  43. list.each do |data|
  44. print_data data, columns
  45. end
  46.  
  47. print_all
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement