Module: MoreCoreExtensions::ArrayTableize

Defined in:
lib/more_core_extensions/core_ext/array/tableize.rb

Instance Method Summary collapse

Instance Method Details

#tableize(options = {}) ⇒ Object

Create a string representation of self in a tabular format if self is an Array of Arrays or an Array of Hashes.

If an Array of Hashes is passed, the headers are assumed from the keys in the first element of the Array, and the header option is ignored. Also, the headers are sorted, unless overridden with options.

General options: ::max_width: Maximum column width, in order to limit wide columns.

Options with Array of Arrays: ::header: Whether or not the first row of data is a header row. Default is

true.

Options with Array of Hashes: ::columns: An Array of keys that define the order of all columns. ::leading_columns: An Array of keys that should be moved to the left side

of the table.  This option is ignored if columns option
is passed.

::trailing_columns: An Array of keys that should be moved to the right side

                  of the table.  This option is ignored if columns option
                  is passed.

[["Col1", "Col2"], ["Val1", "Val2"], ["Value3", "Value4"]].tableize #=>

 Col1   | Col2
--------+--------
 Val1   | Val2
 Value3 | Value4


33
34
35
36
37
38
39
# File 'lib/more_core_extensions/core_ext/array/tableize.rb', line 33

def tableize(options = {})
  case self.first
  when Array; tableize_arrays(options)
  when Hash;  tableize_hashes(options)
  else raise "must be an Array of Arrays or Array of Hashes"
  end
end