Dharam Gollapudi
Dharam Gollapudi

Reputation: 6438

rails cycle helper with percentages

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?

Upvotes: 1

Views: 265

Answers (1)

Dumitru Ceban
Dumitru Ceban

Reputation: 640

Try to take a look at this: http://snippets.dzone.com/posts/show/4571

Upvotes: 1

Related Questions