Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/fusebox/core_ext/array.rb
Instance Method Summary collapse
-
#to_ascii_table(keys, labels) ⇒ String
Return an ASCII table for an array of hashes.
Instance Method Details
#to_ascii_table(keys, labels) ⇒ String
Return an ASCII table for an array of hashes
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 34 35 36 37 38 |
# File 'lib/fusebox/core_ext/array.rb', line 9 def to_ascii_table (keys, labels) raise ArgumentError, "`labels` length does not match `keys` length" if keys.length != labels.length return '' if self.length == 0 raise ArgumentError, "expected elements to be Hash instances; instead received: #{first.inspect}" unless first.is_a?(Hash) widths = keys.map do |key| ([Hash[keys.zip(labels)]] + self).map { |row| row[key].to_s.length }.max end text = '' text += to_ascii_table_hr(widths) labels.each_with_index do |key, i| text += "| " + labels[i].ljust(widths[i] + 1) end text += "|\n" text += to_ascii_table_hr(widths) each do |row| keys.each_with_index do |key, i| text += "| " + row[key].ljust(widths[i] + 1) end text += "|\n" end text += to_ascii_table_hr(widths) text end |