Haml new tr every 3 items

I have the following code (simplified to get to the point):

- @assumptions[1].each do |ca| - if count % 3 == 1 %tr %th = ca.value - else %th = ca.value - count = count + 1 

I'm not sure how to make this work in haml so that every 4 elements create a new tag.

Here is how it outputs:

  <tr> <tr> <th> 700.0 </th> </tr> <th> 1235.0 </th> <th> 0.8 </th> <th> 650.0 </th> <tr> <th> 1050.0 </th> </tr> <th> 0.2 </th> </tr> 

This is how I would like to output it:

 <tr> <th> 700.0 </th> <th> 1235.0 </th> <th> 0.8 </th> </tr> <tr> <th> 650.0 </th> <th> 1050.0 </th> </tr> 

Hope this makes sense.

+6
source share
1 answer

You can use the Enumerable each_slice method to group them into pieces 3:

 - @assumptions[1].each_slice(3) do |group| %tr - group.each do |ca| %th= ca.value 
+22
source

Source: https://habr.com/ru/post/890522/


All Articles