/ Published in: Rails
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
%w(1 2 3 4 5 6 7).in_groups_of(3, ' ') {|group| p "<tr>" << (group.collect! {|x| "<td>#{x}</td>" }.to_s) << "</tr>" } # returns: #"<tr><td>1</td><td>2</td><td>3</td></tr>" #"<tr><td>4</td><td>5</td><td>6</td></tr>" #"<tr><td>7</td><td> </td><td> </td></tr>" # The better rails way: <table> <% %w(1 2 3 4 5 6 7).in_groups_of(3, ' ') do |group| %> <%= content_tag(:tr, (group.collect! {|x| content_tag(:td, "#{x} hoo!") } ) ) %> <% end %> </table> # returns: # <table> # <tbody> # <tr><td>1 hoo!</td><td>2 hoo!</td><td>3 hoo!</td></tr> # <tr><td>4 hoo!</td><td>5 hoo!</td><td>6 hoo!</td></tr> # <tr><td>7 hoo!</td><td> hoo!</td><td> hoo!</td></tr> # </tbody> # </table>