-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create table directly from Hash #19
Labels
Comments
Hi @vaibhavatul47; if you have a patch for this completed already, let me know - otherwise I may tackle this. |
I had it somewhere on my machine, I'll share the patch shortly. |
I know this is old, but @vaibhavatul47 this would be super nice to have :) |
No change to the library is required. It has established its contract w/r/t its API. Just convert your data to match the contract like this: #!/usr/bin/env ruby
# convert_hash_to_data.rb
require 'tty-table'
headers = [:key1, :key2, :key3]
array_of_hashes = [
{
key1: 'one',
key2: 'two'
},
{
key1: 'one',
key2: 'two',
key3: 'three'
},
{
key2: 'two',
key3: 'three'
},
{
key3: 'three'
}
]
def convert_hash_to_data(headers, array_of_hashes)
data = []
array_of_hashes.each do |entry|
row = []
headers.each do |key|
row << entry[key]
end
data << row
end
data
end
data = convert_hash_to_data(headers, array_of_hashes)
table = TTY::Table.new(headers, data)
puts table
__END__
produces this output ...
key1 key2 key3
one two
one two three
two three
three |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be great to add support for data saved in array of hash. Eg:
Output:
Current implementation expects each header to have value defined for each row. If values are not available then it is to be replaced by
nil
. For above example sample input would be:I can submit a PR if required.
The text was updated successfully, but these errors were encountered: