Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_columns list
- list.map{|data| data.keys}.flatten.uniq
- end
- def print_line items
- @lines_to_print << items
- end
- def print_columns columns
- print_line columns.map(&:to_s)
- print_line ["_"]* columns.count
- end
- def print_data data, columns
- print_line columns.map{|col| data[col].to_s}
- end
- def print_all
- max_width = []
- @lines_to_print.each do |line|
- line.each_with_index do |col_val, index|
- if (max_width[index]||0)< col_val.size
- max_width[index] = col_val.size
- end
- end
- end
- @lines_to_print.each do |line|
- line.each_with_index do |item, index|
- print item.rjust(max_width[index])
- print "|"
- end
- print "\n"
- end
- end
- def table list
- @lines_to_print = []
- list = Array(list).map(&:to_hash)
- columns = get_columns list
- print_columns columns
- list.each do |data|
- print_data data, columns
- end
- print_all
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement