0

Imagine I have array of values ranging from 1 to 24. I want to populate these values in an HTML table as shown below.

HTML Table

| 1 | 2 | 3 | 4 | 5 | 6 |
| 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 |

Can anybody help me in figuring out how to loop through an array in ruby and populate these values? I am using ruby 1.8.7.

P.S Sorry about representing the HTML table in an ugly way.

2 Answers 2

3

Use each_slice:

1.8.7 (main):0 > (1..24).each_slice(6).each {|b| p b }
[1, 2, 3, 4, 5, 6]
[7, 8, 9, 10, 11, 12]
[13, 14, 15, 16, 17, 18]
[19, 20, 21, 22, 23, 24]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks !! Im marking your answer as correct even though I have posted the exact code i was looking for below.
0

This is more of an actual representation of what I exactly needed which is totally based on lucapette's answer. Thanks to him.

<table>

    <% (1..24).each_slice(6).each do | num |    %>
    <tr>    
    <% num.each do |n| %>
        <td> <%= n %></td>
     <% end %>
    </tr>
     <% end %>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.