我的应用程序返回一个键和值的散列,我希望它能以一种易于阅读的方式显示它们。有没有办法让它在显示信息的同时显示密钥。因此,如果我搜索一个菌株,它将返回类似于下面的内容
ID: 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选择= AttributeParser.new("http://strainapi.evanbusse.com/rvxnT8j/strains/search/#{first_choice}/#{encoded_second_choice}")
结果= selection.parse_json
将results.map {|x| x.values}.uniq
what I've tried and its results
```javascript选择全部返回大量数据,做好准备!
您想要搜索哪种类型的属性?风味
你想找哪种口味的?如果没有返回任何内容,则表示您的搜索词不被接受。咖啡
strainer.rb:21:警告: URI.escape已过时
156
Bhang巧克力
sativa
咖啡
262
黑色'84
籼稻
咖啡
353
蓝牛
籼稻
咖啡
369
蓝色僵尸
籼稻
咖啡
478
焦糖糖果库什
混合
咖啡
479
焦糖科纳咖啡库什
籼稻
咖啡
480
Caramelicious
混合
咖啡
502
芝士汉堡
混合
咖啡
549
巧克力库什
籼稻
咖啡
555
巧克力
sativa
咖啡
641
空行母库什
籼稻
咖啡
897
大猩猩饼干
籼稻
咖啡
1008
头部跳跃
混合
咖啡
1112
杰西的女孩
籼稻
咖啡
1149
Karma Bitch
混合
咖啡
1153
凯利·希尔黄金
籼稻
咖啡
1196
LA巧克力
混合
咖啡
1199
LA Kookies
混合
咖啡
1225
李·罗伊
籼稻
咖啡
1274
逻辑柴油
混合
咖啡
1296
Madman OG
混合
咖啡
1593
98年前的Bubba Kush
籼稻
咖啡
1745
尊贵的公主
混合
咖啡
1762
大脚野人Sap
混合
咖啡
1876年
超级猫咪尿液
sativa
咖啡
1970年
公牛
籼稻
咖啡
2022年
松露黄油
籼稻
咖啡
20:09:17过滤器(主)`
发布于 2020-08-06 12:05:24
有一个很棒的控制台库,叫做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 |
| ... | ... | ... | ... |
+-----+--------------------------+--------+--------+https://stackoverflow.com/questions/63275102
复制相似问题