Beautiful, aligned table output for Ruby hashes and objects with zero dependencies.
- π Zero runtime dependencies
- π Automatic column width calculation
- π― Smart type formatting (booleans, nil, strings)
- π Column alignment (left, right, center)
- π¨ Custom column headers
- π Column filtering
- π Preserves original column order
- π Markdown table output (v0.2.0+)
- π Optional color output via
pastelgem (v0.3.0+) - π€ CSV export with Excel-compatible encoding (v0.4.0+)
Add this line to your application's Gemfile:
gem 'typed_print'
Or install it yourself:
gem install typed_print
require 'typed_print'
data = [
{ name: "Alice", score: 100, active: true },
{ name: "Bob", score: 42, active: false }
]
TypedPrint.print(data)Output:
Name Score Active
------+------+-------
Alice 100 true
Bob 42 false
TypedPrint.print(data, format: :markdown)Output:
| Name | Score | Active |
|-------|-------|--------|
| Alice | 100 | true |
| Bob | 42 | false |
TypedPrint.print(data, align: { score: :right })Output:
Name Score Active
------+------+-------
Alice 100 true
Bob 42 false
TypedPrint.print(data, only: [:name, :score])Output:
Name Score
------+------
Alice 100
Bob 42
TypedPrint.print(data, headers: { name: "Username", score: "Points", active: "Status" })Output:
Username Points Status
---------+------+-------
Alice 100 true
Bob 42 false
table_string = TypedPrint.table(data)
puts table_string.upcase
# Markdown format
markdown_string = TypedPrint.table(data, format: :markdown)
File.write("table.md", markdown_string)mixed_data = [
{ name: "Product A", price: 29.99, in_stock: true, notes: nil },
{ name: "Product B", price: 49.99, in_stock: false, notes: "Limited edition" }
]
TypedPrint.print(mixed_data)Output:
Name Price In_stock Notes
----------+-------+---------+-------------
Product A 29.99 true
Product B 49.99 false Limited edition
TypedPrint.print(data, options) Prints the formatted table to stdout and returns nil.
Options:
align: Hash- Column alignment (:left,:right,:center), defaults to:leftonly: Array- Array of column symbols to displayheaders: Hash- Custom headers for columnsformat: Symbol- Output format (:plainor:markdown), defaults to:plaincolor: Boolean- Auto color by type (headers cyan, numbers/true green, false red, nil gray), requirespastelgemcolors: Hash- Manual per-column color map (e.g.{ name: :cyan, score: :green }), requirespastelgem
TypedPrint.table(data, options) returns the formatted table as a string.
Same options as print.
TypedPrint.to_csv(data, options) returns data as a CSV string.
Options:
only: Array- Columns to includeheaders: Hash- Custom column headersdelimiter: String- Column separator (default:",")
TypedPrint.save(data, path, options) writes a CSV file with UTF-8 BOM and returns nil.
Same options as to_csv.
Color support is optional and requires the pastel gem. Add it to your Gemfile:
gem 'pastel'Automatic coloring by type:
TypedPrint.print(data, color: true)
# Headers β cyan, Integer/Float/true β green, false β red, nil β grayManual per-column colors:
TypedPrint.print(data, colors: { name: :cyan, score: :green, active: :yellow })Both :plain and :markdown formats support color. If pastel is not installed, color options are silently ignored and output is plain text.
No extra dependencies β uses Ruby's stdlib csv.
Return CSV string:
csv = TypedPrint.to_csv(data)
# => "Name,Score,Active\nAlice,100,true\nBob,42,false\n"Save to file (Excel-compatible UTF-8 BOM):
TypedPrint.save(data, "output.csv")Custom delimiter:
TypedPrint.to_csv(data, delimiter: ";")
TypedPrint.save(data, "output.csv", delimiter: ";")Filter columns and custom headers:
TypedPrint.to_csv(data, only: [:name, :score], headers: { score: "Points" })After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests.
To install this gem onto your local machine, run bundle exec rake install.
Bug reports and pull requests are welcome on GitHub at https://github.com/yourusername/typed_print.
The gem is available as open source under the terms of the MIT License.
