class PrettyTable::Table
- PrettyTable::Table
- Reference
- Object
Defined in:
prettytable/table.crConstructors
-
.new(headers : Array(String))
Create a new
Tablewith the specified headers. -
.new
Creates a new
Tablewith no headers.
Class Method Summary
-
.from_csv(csv_file : String, sep : Char = ',') : PrettyTable::Table
Returns a table created based on data from a .csv file.
Instance Method Summary
-
#-(other : PrettyTable::Table) : PrettyTable::Table
Returns a new
Tablethat is a copy ofself, removing any items that appear in other. -
#<<(row : Array(String))
Adds a row to the table.
-
#<<(rows : Array(Array(String)))
Adds multiple rows to the table.
-
#[](idx : Int32) : Array(String)
Returns the row at index idx.
-
#[](range : Range) : Array(Array(String))
Returns all rows within the given range.
-
#[](key : String) : Array(String)
Returns table column [key].
-
#[]=(idx : Int32, value : Array(String))
Updates the row at the given index.
-
#add_column(column_name : String, column_data : Array(String))
Appends a new column to
self. -
#add_row(row : Array(String))
Adds a row to the table.
-
#add_rows(rows : Array(Array(String)))
Adds multiple rows to the table.
-
#clear
Removes all rows from
self. -
#delete_row(idx : Int32) : Array(String)
Removes a row from the table, returning that row.
-
#empty? : Bool
Returns
trueifselfis empty,falseotherwise. -
#headers : Array(String)
Returns the table headers.
-
#remove_column(column_name : String) : Array(String)
Removes a column from
selfand returns the data from the removed column. -
#rows : Array(Array(String))
Returns the rows in the table.
-
#select(columns : Array(String)) : PrettyTable::Table
Returns a new
Tablewith the given columns. -
#set_headers(headers : Array(String))
Sets the table headers.
-
#sort(column : String, asc_order = true) : PrettyTable::Table
Sorts a table based on the given column and returns a new
Table. -
#sort(&block : Array(String), Array(String) -> Int32) : PrettyTable::Table
Returns a new
Tablewith rows sorted based on the comparator in the given block. -
#to_csv(filename : String, sep : Char = ',')
Saves the table to a .csv file.
-
#to_h : Hash(String, Array(String))
Returns the table as a hash.
-
#to_json : String
Serializes the table into JSON.
-
#to_s(io : IO) : Nil
Writes the table to io.
Constructor Detail
Class Method Detail
Returns a table created based on data from a .csv file.
Instance Method Detail
Returns a new Table that is a copy of self, removing
any items that appear in other.
Adds a row to the table.
An ArgumentError is raised if the row does not
have the same size as the headers.
Returns all rows within the given range.
NOTE See https://crystal-lang.org/api/1.0.0/Array.html#-instance-method for more information.
Adds a row to the table.
An ArgumentError is raised if the row does not
have the same size as the headers.
Removes a column from self and returns the data from the removed column.
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.
Sets the table headers.
An ArgumentError is raised if headers has already been set.
Sorts a table based on the given column and returns a new Table.
Returns a new Table with rows sorted based on the comparator in the given block.