如何显示数组中的TALLY值?
我正在尝试显示数组中的值作为表格格式,
@array = {"Very Good"=>24, "Good"=>81, "Regular"=>3, "Bad"=>1, "Very Bad"=>1}
我的数组显示为显示,但是我想显示信息作为表格式,
<table>
<tr>
<td>Name</td>
<td>Count</td>
</tr>
<% @array.each do |info|%>
<tr>
<td><% info[0].try("name") %></td>
<td><% info[0].try("count") %></td>
</tr>
<% end %>
</table>
我尝试了此代码,但是它以行显示为行,我想显示分开的值。
<% @array.map do |info| %>
<%= info %>
<% end %>
这是视图显示,但我试图以列名和列计数为表格(有组织)。
<%= @array.tally %>
它用括号显示:
{"Very Good"=>24, "Good"=>81, "Regular"=>3, "Bad"=>1, "Very Bad"=>1}
I'm trying to display values from array as table format
@array = {"Very Good"=>24, "Good"=>81, "Regular"=>3, "Bad"=>1, "Very Bad"=>1}
I have this array displaed but i want to display information as table format
<table>
<tr>
<td>Name</td>
<td>Count</td>
</tr>
<% @array.each do |info|%>
<tr>
<td><% info[0].try("name") %></td>
<td><% info[0].try("count") %></td>
</tr>
<% end %>
</table>
I tried this code but it's displaying the information as row and i want to display values separated.
<% @array.map do |info| %>
<%= info %>
<% end %>
Here is the view display but i'm trying to display as table format (organized) with column name and column count.
<%= @array.tally %>
It displays with brackets:
{"Very Good"=>24, "Good"=>81, "Regular"=>3, "Bad"=>1, "Very Bad"=>1}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还忘记了
=
显示数据&lt;%=
不是&lt;%
。you also forgot the
=
to display the data<%=
not<%
.#当迭代哈斯时,需要两个占位持有人变量来表示每个键/值对。
用
hash
与值sort_by {| k,v |进行排序。 V}
。如果用键排序,则可以更改sort_by {| k,v | k}
#When iterating over hashes, two placeholder variables are needed to represent each key/value pair.
Sort by with
hash
with valuesort_by{|k, v| v}
. If you sort with key, you can changesort_by{|k, v| k}