带百分比的 Rails 循环助手
Rails 循环助手在迭代中的给定值之间循环:
# Alternate CSS classes for even and odd numbers...
@items = [1,2,3,4]
<table>
<% @items.each do |item| %>
<tr class="<%= cycle("even", "odd") -%>">
<td>item</td>
</tr>
<% end %>
</table>
是否可以实现类似循环的功能,但具有类似于给定哈希的百分比,并包含以下信息:
{
'red' => 40,
'blue' => 20,
'green' => 40
}
如果表有 10 行,我希望 4 行是红色,2 行是蓝色,4 行是绿色,但随机分布。
实现这一目标的最佳方法是什么?
Rails cycle helper cycles between the given values within a iteration:
# Alternate CSS classes for even and odd numbers...
@items = [1,2,3,4]
<table>
<% @items.each do |item| %>
<tr class="<%= cycle("even", "odd") -%>">
<td>item</td>
</tr>
<% end %>
</table>
Is it possible achieve cycle like functionality but with percentages like given a hash with the following info:
{
'red' => 40,
'blue' => 20,
'green' => 40
}
if a table has 10 rows, I want 4 be red, 2 be blue and 4 be green, but randomly distributed.
What is the best way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试看看这个: http://snippets.dzone.com/posts/show/4571
Try to take a look at this: http://snippets.dzone.com/posts/show/4571