Class: Table
- Inherits:
-
Array
show all
- Defined in:
- lib/charlie/etc/minireport.rb
Instance Method Summary
collapse
Methods inherited from Array
#at_rand, #average, #dot_product, #find_index, #map_with_index, #rand_index, #shuffle, #shuffle!, #stats, #stddev, #swap_element_at_index!, #to_table
Constructor Details
#initialize(tbl, colnames = nil) ⇒ Table
Returns a new instance of Table.
11
12
13
14
|
# File 'lib/charlie/etc/minireport.rb', line 11
def initialize(tbl,colnames=nil)
@colnames = colnames || tbl[0].map_with_index{|e,i|i+1}
super(tbl.map{|row|row.dup.map(&:to_s)})
end
|
Instance Method Details
#to_csv ⇒ Object
23
24
25
|
# File 'lib/charlie/etc/minireport.rb', line 23
def to_csv
map{|r| r.map(&:inspect).join(', ') }.join("\n")
end
|
#to_html ⇒ Object
16
17
18
19
20
21
|
# File 'lib/charlie/etc/minireport.rb', line 16
def to_html
"<table>\n" +
"<tr>" + @colnames.map{|t| "<th>#{t}</th>" }.join + "</tr>\n" +
map{|r| "<tr>\n" + r.map{|e| "\t<td>#{e.gsub('<','<').gsub("\r\n",'<br>')}</td>\n" }.join + "</tr>\n" }.join +
"</table>"
end
|
#to_s(csz = nil) ⇒ Object
27
28
29
30
31
32
33
34
35
|
# File 'lib/charlie/etc/minireport.rb', line 27
def to_s(csz=nil)
console_size = 80
rsz = at(0).size
csz ||= [(console_size-rsz) / rsz]*rsz
pad = ' ' * csz.max
sep = csz.map{|x|'-'*x}.join('+')
([sep,@colnames.zip_with(csz){|str,sz| (str+pad)[0...sz] }.join('|'),sep]+
map{|r| r.zip_with(csz){|str,sz| (str.gsub("\r\n","\\")+pad)[0...sz] }.join('|') } << sep).join("\n")
end
|