根据hirb docs,AutoTable.render()
接受一个可以是 array_of_arrays 或 array_of_hashes 的参数。但是因为您的 hash 参数有效,所以我查看了您的表输出,并决定尝试仅添加一个选项来更改您获得的列名:
require 'hirb'
freqs = {
'go' => 3,
'no' => 4,
'to' => 1
}
puts Hirb::Helpers::AutoTable.render(
freqs,
fields: [1, 0], #Specify which fields to include in the table and their order.
#For a row that is an array, the field names are the integers 0, 1, 2, etc.
#For a row that is a hash, the field names are the keys.
headers: {0 => 'Word', 1 => 'Frequency'}, #Convert the field names to something more desirable for the column headers
description: false #Get rid of "3 rows in set" following the table
)
--output:--
+-----------+------+
| Frequency | Word |
+-----------+------+
| 3 | go |
| 4 | no |
| 1 | to |
+-----------+------+
有效。必须发生的是:AutoTable.render()
需要一个数组——array_of_arrays 或 array_of_hashes——并且如果该方法没有将数组作为参数,它会调用to_a()
该参数。看看你的哈希会发生什么:
~/ruby_programs$ irb
2.4.0 :001 > freqs = {'go' => 3, 'no' => 4, 'to' => 1}
=> {"go"=>3, "no"=>4, "to"=>1}
2.4.0 :002 > freqs.to_a
=> [["go", 3], ["no", 4], ["to", 1]]
有AutoTable.render()
需要的array_of_arrays。稍微重新排列一下,array_of_arrays 看起来像这样:
index 0 index 1
| |
[ V V
["go", 3], #row array
["no", 4],
["to", 1]
]
对于array_of_arrays,表中的列标题是每个行数组中的索引位置。选项AutoTable.render()
允许您指定要包含在表中的列/索引位置及其顺序,选项允许您将列标题转换为更理想的内容。
这是一个更一般的例子:
require 'hirb'
require 'pp'
data = [
['go', 1, '1/12/18'],
['to', 4, '1/24/18'],
['at', 2, '1/28/18']
]
puts Hirb::Helpers::AutoTable.render(
data,
fields: [2, 0], #Specify the index positions in each row array to include in the table and their column order in the table
headers: {0 => 'Word', 2 => 'Date'}, #Convert the column headers to something more desirable
description: false #Get rid of "3 rows in set" following the table
)
--output:--
+---------+------+
| Date | Word |
+---------+------+
| 1/12/18 | go |
| 1/24/18 | to |
| 1/28/18 | at |
+---------+------+
====
require 'hirb'
require 'pp'
freqs = {
'go' => 3,
'no' => 4,
'to' => 1
}
col_names = %w[word count]
new_freqs = freqs.map do |key, val|
{col_names[0] => key, col_names[1] => val}
end
pp new_freqs
puts Hirb::Helpers::AutoTable.render(
new_freqs,
fields: ['word', 'count'], #Specify which keys to include in table and their column order.
headers: {'word' => 'Good Word', 'count' => 'Frequency'}, #Convert keys to more desirable headers.
description: false #Get rid of "3 rows in set" following the table
)
--output:--
[{"word"=>"go", "count"=>3},
{"word"=>"no", "count"=>4},
{"word"=>"to", "count"=>1}]
+-----------+-----------+
| Good Word | Frequency |
+-----------+-----------+
| go | 3 |
| no | 4 |
| to | 1 |
+-----------+-----------+
====
require 'hirb'
require 'pp'
freqs = {
'go' => 3,
'no' => 4,
'to' => 1
}
col_names = %i[word count]
new_freqs = freqs.map do |key, val|
{col_names[0] => key, col_names[1] => val}
end
pp new_freqs
puts Hirb::Helpers::AutoTable.render(new_freqs)
--output:--
[{:word=>"go", :count=>3}, {:word=>"no", :count=>4}, {:word=>"to", :count=>1}]
+-------+------+
| count | word |
+-------+------+
| 3 | go |
| 4 | no |
| 1 | to |
+-------+------+
3 rows in set
===
require 'hirb'
require 'pp'
data = {
'first' => 'second',
'third' => 'fourth',
'fifth' => 'sixth'
}
col_names = %i[field1 field2]
new_data = data.map do |key, val|
{col_names[0] => key, col_names[1] => val}
end
pp new_data
puts Hirb::Helpers::AutoTable.render(new_data)
--output:--
[{:field1=>"first", :field2=>"second"},
{:field1=>"third", :field2=>"fourth"},
{:field1=>"fifth", :field2=>"sixth"}]
+--------+--------+
| field1 | field2 |
+--------+--------+
| first | second |
| third | fourth |
| fifth | sixth |
+--------+--------+
3 rows in set
=====
require 'hirb'
data = [
{field1: 'first', field2: 'second'},
{field1: 'third', field2: 'fourth'},
{field1: 'fifth', field2: 'sixth'}
]
puts Hirb::Helpers::AutoTable.render(data)
--output:--
+--------+--------+
| field1 | field2 |
+--------+--------+
| first | second |
| third | fourth |
| fifth | sixth |
+--------+--------+
3 rows in set