使用 map 或 each 提高我的 CLI 应用程序哈希 returns 的可读性

Improve readability in my CLI application's hash returns using map or each

我的应用程序 return 是键和值的散列,我希望它以易于阅读的方式显示它们。有没有办法让它显示密钥和信息。因此,如果我搜索菌株,它会 return 类似于此

ID:身份证号 姓名:姓名 种族:种族 风味:风味 等等

打印内容示例:

Choosing ALL returns a LOT of data, be ready for it!
By what strain attribute would you like to search? Flavor
Which Flavor would you like to search for? If nothing is returned, your search term isn't one thats accepted. coffee
strainer.rb:21: warning: URI.escape is obsolete
{"id"=>156, "name"=>"Bhang Chocolope", "race"=>"sativa", "flavor"=>"Coffee"}
{"id"=>262, "name"=>"Black '84", "race"=>"indica", "flavor"=>"Coffee"}
{"id"=>353, "name"=>"Blue Ox", "race"=>"indica", "flavor"=>"Coffee"}
{"id"=>369, "name"=>"Blue Zombie", "race"=>"indica", "flavor"=>"Coffee"}
{"id"=>478, "name"=>"Caramel Candy Kush", "race"=>"hybrid", "flavor"=>"Coffee"}      
{"id"=>479, "name"=>"Caramel Kona Coffee Kush", "race"=>"indica", "flavor"=>"Coffee"}
{"id"=>480, "name"=>"Caramelicious", "race"=>"hybrid", "flavor"=>"Coffee"}
{"id"=>502, "name"=>"Cheeseburger", "race"=>"hybrid", "flavor"=>"Coffee"}
{"id"=>549, "name"=>"Chocolate Kush", "race"=>"indica", "flavor"=>"Coffee"}
{"id"=>555, "name"=>"Chocolope", "race"=>"sativa", "flavor"=>"Coffee"}
{"id"=>641, "name"=>"Dakini  Kush", "race"=>"indica", "flavor"=>"Coffee"}
{"id"=>897, "name"=>"Gorilla Biscuit", "race"=>"indica", "flavor"=>"Coffee"}
{"id"=>1008, "name"=>"Head Trip", "race"=>"hybrid", "flavor"=>"Coffee"}
{"id"=>1112, "name"=>"Jesse's Girl", "race"=>"indica", "flavor"=>"Coffee"}
{"id"=>1149, "name"=>"Karma Bitch", "race"=>"hybrid", "flavor"=>"Coffee"}
{"id"=>1153, "name"=>"Kelly Hill Gold", "race"=>"indica", "flavor"=>"Coffee"}
{"id"=>1196, "name"=>"LA Chocolat", "race"=>"hybrid", "flavor"=>"Coffee"}
{"id"=>1199, "name"=>"LA Kookies", "race"=>"hybrid", "flavor"=>"Coffee"}
{"id"=>1225, "name"=>"Lee Roy", "race"=>"indica", "flavor"=>"Coffee"}
{"id"=>1274, "name"=>"Logic Diesel", "race"=>"hybrid", "flavor"=>"Coffee"}
{"id"=>1296, "name"=>"Madman OG", "race"=>"hybrid", "flavor"=>"Coffee"}
{"id"=>1593, "name"=>"Pre-98 Bubba Kush", "race"=>"indica", "flavor"=>"Coffee"}
{"id"=>1745, "name"=>"Royal Highness", "race"=>"hybrid", "flavor"=>"Coffee"}
{"id"=>1762, "name"=>"Sasquatch Sap", "race"=>"hybrid", "flavor"=>"Coffee"}
{"id"=>1876, "name"=>"Super Cat Piss", "race"=>"sativa", "flavor"=>"Coffee"}
{"id"=>1970, "name"=>"The OX", "race"=>"indica", "flavor"=>"Coffee"}
{"id"=>2022, "name"=>"Truffle Butter", "race"=>"indica", "flavor"=>"Coffee"}```

This is what I have so far: 

```ruby
selection = AttributeParser.new("http://strainapi.evanbusse.com/rvxnT8j/strains/search/#{first_choice}/#{encoded_second_choice}")

results = selection.parse_json

puts results.map {|x| x.values}.uniq

我的尝试及其结果

Choosing ALL returns a LOT of data, be ready for it!
By what strain attribute would you like to search? Flavor
Which Flavor would you like to search for? If nothing is returned, your search term isn't one thats accepted. coffee
strainer.rb:21: warning: URI.escape is obsolete
156
Bhang Chocolope
sativa
Coffee
262
Black '84      
indica
Coffee
353
Blue Ox        
indica
Coffee
369
Blue Zombie    
indica
Coffee
478
Caramel Candy Kush
hybrid
Coffee
479
Caramel Kona Coffee Kush
indica
Coffee
480
Caramelicious
hybrid
Coffee
502
Cheeseburger
hybrid
Coffee
549
Chocolate Kush
indica
Coffee
555
Chocolope
sativa
Coffee
641
Dakini  Kush
indica
Coffee
897
Gorilla Biscuit
indica
Coffee
1008
Head Trip
hybrid
Coffee
1112
Jesse's Girl
indica
Coffee
1149
Karma Bitch
hybrid
Coffee
1153
Kelly Hill Gold
indica
Coffee
1196
LA Chocolat
hybrid
Coffee
1199
LA Kookies
hybrid
Coffee
1225
Lee Roy
indica
Coffee
1274
Logic Diesel
hybrid
Coffee
1296
Madman OG
hybrid
Coffee
1593
Pre-98 Bubba Kush
indica
Coffee
1745
Royal Highness
hybrid
Coffee
1762
Sasquatch Sap
hybrid
Coffee
1876
Super Cat Piss
sativa
Coffee
1970
The OX
indica
Coffee
2022
Truffle Butter
indica
Coffee
[20:09:17]  strainer (master)```

有一个名为 Hirb 的很棒的控制台库,可用于此目的。 例如,假设您的哈希数组在 data 中,我们可以这样做:

require 'hirb'
puts Hirb::Helpers::AutoTable.render(data, fields: %w|id name flavor race|)

结果:

+-----+--------------------------+--------+--------+
| id  | name                     | flavor | race   |
+-----+--------------------------+--------+--------+
| 156 | Bhang Chocolope          | Coffee | sativa |
| 262 | Black '84                | Coffee | indica |
| 353 | Blue Ox                  | Coffee | indica |
| 369 | Blue Zombie              | Coffee | indica |
| 478 | Caramel Candy Kush       | Coffee | hybrid |
| 479 | Caramel Kona Coffee Kush | Coffee | indica |
| 480 | Caramelicious            | Coffee | hybrid |
| 502 | Cheeseburger             | Coffee | hybrid |
| 549 | Chocolate Kush           | Coffee | indica |
| 555 | Chocolope                | Coffee | sativa |
| 641 | Dakini  Kush             | Coffee | indica |
| 897 | Gorilla Biscuit          | Coffee | indica |
| ... | ...                      | ...    | ...    |
+-----+--------------------------+--------+--------+