我有这个哈希,包含哈希的数组:
{
"Company1" => [
{ "name" => "test1", "count" => 2},
{ "name" => "test123", "count" => 3}
],
"Company2" => [
{ "name" => "test33", "count" => 2},
{ "name" => "test453", "count" => 3},
{ "name" => "test342", "count" => 2},
{ "name" => "test90", "count" =>3}
]
}
我想迭代它,所以我可以使格式以表格格式打印出来。表格式可能是这样的:
Company | Name | Count
Company1
test1 2
test123 3
Company2
test33 2
test453 3
test342 2
test90 3
那么我如何迭代包含哈希数组的ruby哈希。
答案 0 :(得分:1)
尝试这样的事情:
hash.each do |company, elements|
puts "#{company}\t\t"
elements.each do |v|
puts "\t#{v['name']}\t#{v['count']}"
end
end