class PrettyTable::Table

Defined in:

prettytable/table.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(headers : Array(String)) #

Create a new Table with the specified headers.


[View source]
def self.new #

Creates a new Table with no headers.


[View source]

Class Method Detail

def self.from_csv(csv_file : String, sep : Char = ',') : PrettyTable::Table #

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


[View source]

Instance Method Detail

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


[View source]
def <<(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.


[View source]
def <<(rows : Array(Array(String))) #

Adds multiple rows to the table.


[View source]
def [](idx : Int32) : Array(String) #

Returns the row at index idx.


[View source]
def [](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.


[View source]
def [](key : String) : Array(String) #

Returns table column [key].


[View source]
def []=(idx : Int32, value : Array(String)) #

Updates the row at the given index.


[View source]
def add_column(column_name : String, column_data : Array(String)) #

Appends a new column to self.


[View source]
def 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.


[View source]
def add_rows(rows : Array(Array(String))) #

Adds multiple rows to the table.


[View source]
def clear #

Removes all rows from self.


[View source]
def delete_row(idx : Int32) : Array(String) #

Removes a row from the table, returning that row.


[View source]
def empty? : Bool #

Returns true if self is empty, false otherwise.


[View source]
def headers : Array(String) #

Returns the table headers.


[View source]
def remove_column(column_name : String) : Array(String) #

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


[View source]
def rows : Array(Array(String)) #

Returns the rows in the table.


[View source]
def 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.


[View source]
def set_headers(headers : Array(String)) #

Sets the table headers.

An ArgumentError is raised if headers has already been set.


[View source]
def sort(column : String, asc_order = true) : PrettyTable::Table #

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


[View source]
def sort(&block : Array(String), Array(String) -> Int32) : PrettyTable::Table #

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


[View source]
def to_csv(filename : String, sep : Char = ',') #

Saves the table to a .csv file.


[View source]
def to_h : Hash(String, Array(String)) #

Returns the table as a hash.


[View source]
def to_json : String #

Serializes the table into JSON.


[View source]
def to_s(io : IO) : Nil #

Writes the table to io.


[View source]