class Array
Public Instance Methods
table_data
data
generate a data row as part of a table
# File lib/array/formatter.rb, line 250 def table_data(data) left, middle, right = [@chars[:ldb], @chars[:idb], @chars[:rdb]] a = [] data.each_with_index do |item, x| a << (' ' + item.to_s.send(@align[x] || :ljust, @widths[x]) + ' ') end s = @chars.wrap(left) + a.join(@chars.wrap(middle)) + @chars.wrap(right) + "\n" s end
table_line
position
generate a table line for position (:top, :middle, :bottom)
# File lib/array/formatter.rb, line 234 def table_line(position) c = @chars left, line, mid, right = case position when :top then [c[:tlb], c[:tb], c[:tib], c[:trb]] when :middle then [c[:lib], c[:ib], c[:mib], c[:rib]] when :bottom then [c[:blb], c[:bb], c[:bib], c[:brb]] end s = @chars.wrap(left + @widths.map{|w| line * (w + 2)}.join(mid) + right) + "\n" s end
string = ARRAY.to_csv
Convert an array of arrays to CSV representation Basically, each top-level array row becomes a line of CSV data The 2nd level arrays become individual rows of data, separated by commas. Each 2nd level array item gets quoted if it contains any punctuation characters.
# File lib/array/formatter.rb, line 57 def to_csv map {|row| row.map {|f| f =~ /[[:punct:]]/ ? '"' + f.gsub(/"/, '""') + '"' : f }. join(",")}. join("\n") end
string = ARRAY.to_html indent=0
Return an HTML representation of an array of arrays as an HTML table. If indent given, indent the <table> by that many spaces.
# File lib/array/formatter.rb, line 22 def to_html(indent=0) wrap_map_html_data("table", indent + 0) do |row, rx| row.wrap_map_html_data("tr", indent + 1) do |field, fx| tag = rx == 0 ? 'th' : 'td' (' ' * (indent + 2)) + # indention "<#{tag}>" + # open tag (field && CGI.escapeHTML(field) || '') + # cell text "</#{tag}>\n" # close tag end end end
# File lib/array/formatter.rb, line 193 def to_table(name=:ascii) @chars = TableChars.get name # compute the maximum widths and the alignment of each column @widths = [] @align = [] each_with_index do |row,rx| row.each_with_index do |col,cx| @widths[cx] ||= 0 l = col.to_s.length @widths[cx] = l if l > @widths[cx] if rx == 0 type = nil else type = case (col || '') when /^\$?\s*\d[,\d]*\.\d*$/ then :rjust # real number and/or currency when /^\$?\s*\d[,\d]*$/ then :rjust # integer when /^$/, /^\s*-\s*$/, nil then nil # empty values have no alignment else :ljust end end @align[cx] ||= (col ? type : nil) @align[cx] = :ljust if type && @align[cx] != type && rx > 0 end end # now format each row s = '' each_with_index do |row, rx| s << table_line(rx == 0 ? :top : :middle) s << table_data(row) end s << table_line(:bottom) s end
string = ARRAY.to_yml
Convert an array of arrays to YAML (using built-in core methods)
# File lib/array/formatter.rb, line 69 def to_yml YAML.dump(self) end
string = ARRAY.wrap_map_html_data TAG, INDENT {|DATA, INDEX| block}
Return a string HTML representation of an array of data, starting with <TAG> and ending with </TAG>, and invoking the block on each array item, passing the DATA item with its INDEX.
# File lib/array/formatter.rb, line 40 def wrap_map_html_data(tag, indent=0) s = '' prefix = ' ' * indent s << prefix + "<#{tag}>\n" each_with_index {|data, x| s << ((yield data, x) || '') } s << prefix + "</#{tag}>\n" s end