class

PrettyTable::Table

Inherits Reference < Object

Constructors

new(headers : Array(String))

Create a new Table with the specified headers.

Source
new

Creates a new Table with no headers.

Source

Class methods

from_csv(csv_file : String, sep : Char = ',') : PrettyTable::Table

Returns a table created based on data from a .csv file.

Source

Instance methods

Returns a new Table that is a copy of self, removing any items that appear in other.

Source
<<(row : Array(String))

Adds a row to the table.

An ArgumentError is raised if the row does not have the same size as the headers.

Source
<<(rows : Array(Array(String)))

Adds multiple rows to the table.

Source
[](idx : Int32) : Array(String)

Returns the row at index idx.

Source
[](range : Range) : Array(Array(String))

Returns all rows within the given range.

NOTE: See https://crystal-lang.org/api/1.0.0/Array.html#-instance-method for more information.

Source
[](key : String) : Array(String)

Returns table column [key].

Source
[]=(idx : Int32, value : Array(String))

Updates the row at the given index.

Source
add_column(column_name : String, column_data : Array(String))

Appends a new column to self.

Source
add_row(row : Array(String))

Adds a row to the table.

An ArgumentError is raised if the row does not have the same size as the headers.

Source
add_rows(rows : Array(Array(String)))

Adds multiple rows to the table.

Source
clear

Removes all rows from self.

Source
delete_row(idx : Int32) : Array(String)

Removes a row from the table, returning that row.

Source
empty?

Returns true if self is empty, false otherwise.

Source
headers

Returns the table headers.

Source
remove_column(column_name : String) : Array(String)

Removes a column from self and returns the data from the removed column.

Source
rows

Returns the rows in the table.

Source
select(columns : Array(String)) : PrettyTable::Table

Returns a new Table with the given columns.

Example:

table = PrettyTable::Table.new(["id", "name", "age"])

will create a table with the following columns

+----+------+-----+
| id | name | age |
+----+------+-----+

You can select just the name and age column like this

table.select(["name", "age"])

this will return a new Table with columns

+------+-----+
| name | age |
+------+-----+

However, table.select(["age", "name"]) will create a table with columns

+-----+------+
| age | name |
+-----+------+

NOTE: If no columns are specified (empty array or an array of empty strings) then self is returned.

Source
set_headers(headers : Array(String))

Sets the table headers.

An ArgumentError is raised if headers has already been set.

Source
sort(column : String, asc_order = true) : PrettyTable::Table

Sorts a table based on the given column and returns a new Table.

Source
sort

Returns a new Table with rows sorted based on the comparator in the given block.

Source
to_csv(filename : String, sep : Char = ',')

Saves the table to a .csv file.

Source
to_h

Returns the table as a hash.

Source
to_json

Serializes the table into JSON.

Source
to_s(io : IO) : Nil

Writes the table to io.

Source