Ruby on Rails & Maths...
Posted by Annalisa Afeltra in
Ruby on Rails -
5 comments
I have always been fascinated with Mathematics and how you can solve problems using it, we all know that in programming it is very useful. I recently had to use maths in a layout that I needed to incorporate for a new application…
Problem:
I had to display a dynamic list of suburbs in a table, that was an x amount of rows and had 3 columns.
Solution:
I used Mod (the mathematical arithmetic that gives you the remainder of the division).
Therefore:
<table>
<% r = 0 %>
<% @count_suburb.each do |c| %>
<% area = Area.find_by_code(c[0])%>
<% if ((r % 3) == 0) %>
<tr>
<% end %>
<% r += 1%>
<td>Write something here....<%= c.name.capitalize %></td>
<% if ((r % 3) == 0) %>
</tr>
<% end %>
<% end %>
</table>
Does anyone know of a better solution?
Thanks Annalisa


Comments
Dmytro Shteflyuk
Posted on July 01
Dmytro Shteflyuk
Posted on July 01
Aris
Posted on July 01
Annalisa
Posted on July 01
Christoph Olszowka
Posted on July 02