2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/tablify.rb', line 2
def self.table(array, opts = {})
= array.map(&:keys).flatten.uniq.sort
if opts[:include]
= & opts[:include]
end
if opts[:exclude]
= - opts[:exclude]
end
column_widths = .map(&:length)
array.each do |hash|
.each_with_index do |, i|
l = hash[].to_s.length
if l > column_widths[i]
column_widths[i] = l
end
end
end
lines = [horizontal_rule(column_widths)]
lines << aligned_row(, column_widths)
lines << horizontal_rule(column_widths)
array.each do |row|
row_elements = .map{|h| row[h]}
lines << aligned_row(row_elements, column_widths)
end
lines << horizontal_rule(column_widths)
lines.join("\n")
end
|