[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Looping: break every x times

sshikari

12/17/2006 4:52:00 PM

I have a bunch of pictures to display on a page and I want to do 4 per
row. This code works fine (inserting a <br> every 4th picture):

<% i = 1 %>
<% for photo in @photos do -%>
<a href="/photos/show/<%=photo.id%>"><img
src="/photos/thumb/<%=photo.id%>"/></a>
<% if (i.modulo(4) == 0) %>
<br>
<% end %>
<% i = i+1%>
<% end %>


but I was wondering if there is a more elegant way to do this. Thanks.

4 Answers

Daniel Finnie

12/17/2006 5:04:00 PM

0

That looks like eRb or Ruby on Rails or something but couldn't you just do:

<% @photos.each_with_index do |photo, i| %>
<a href="/photos/show/<%=photo.id%>"><img
src="/photos/thumb/<%=photo.id%>"/></a>
<% if (i % 4).zero? %>
<br>
<% end %>
<%end%>

To get each_with_index you must require 'enumerator' but you could do
Array#each_index and then instead of photo.id you have photos[i].id. Of
course, this all assumes that photos is an array.

Dan

sshikari@gmail.com wrote:
> I have a bunch of pictures to display on a page and I want to do 4 per
> row. This code works fine (inserting a <br> every 4th picture):
>
> <% i = 1 %>
> <% for photo in @photos do -%>
> <a href="/photos/show/<%=photo.id%>"><img
> src="/photos/thumb/<%=photo.id%>"/></a>
> <% if (i.modulo(4) == 0) %>
> <br>
> <% end %>
> <% i = i+1%>
> <% end %>
>
>
> but I was wondering if there is a more elegant way to do this. Thanks.
>
>
>

sshikari

12/17/2006 5:27:00 PM

0

Thanks Daniel, that's what I was looking for.


Daniel Finnie wrote:
> That looks like eRb or Ruby on Rails or something but couldn't you just do:
>
> <% @photos.each_with_index do |photo, i| %>
> <a href="/photos/show/<%=photo.id%>"><img
> src="/photos/thumb/<%=photo.id%>"/></a>
> <% if (i % 4).zero? %>
> <br>
> <% end %>
> <%end%>
>
> To get each_with_index you must require 'enumerator' but you could do
> Array#each_index and then instead of photo.id you have photos[i].id. Of
> course, this all assumes that photos is an array.
>
> Dan
>
> sshikari@gmail.com wrote:
> > I have a bunch of pictures to display on a page and I want to do 4 per
> > row. This code works fine (inserting a <br> every 4th picture):
> >
> > <% i = 1 %>
> > <% for photo in @photos do -%>
> > <a href="/photos/show/<%=photo.id%>"><img
> > src="/photos/thumb/<%=photo.id%>"/></a>
> > <% if (i.modulo(4) == 0) %>
> > <br>
> > <% end %>
> > <% i = i+1%>
> > <% end %>
> >
> >
> > but I was wondering if there is a more elegant way to do this. Thanks.
> >
> >
> >

Daniel Schierbeck

12/17/2006 8:33:00 PM

0

On Mon, 2006-12-18 at 02:04 +0900, Daniel Finnie wrote:
> To get each_with_index you must require 'enumerator' but you could do
> Array#each_index and then instead of photo.id you have photos[i].id.

You don't need to require enumerator to get #each_with_index, at least
not in 1.8.5.


Cheers,
Daniel


Hiccum Blurpaedius

4/16/2013 4:09:00 PM

0

On Monday, April 15, 2013 11:12:46 PM UTC-4, Too_Many_Tools wrote:
> On Apr 15, 10:08 pm, GOD LLC <hic...@gmail.com> wrote:
>
> > On Monday, April 15, 2013 10:49:35 PM UTC-4, Too_Many_Tools wrote:
>
> > > My friends are coming for you.
>
> >
>
> > > So who did it?
>
> >
>
> > > My guess..
>
> >
>
> > > Domestic right wing terrorist.
>
> >
>
> > > NRA member.
>
> >
>
> > > Tea Party connections.
>
> >
>
> > > Your guess?
>
> >
>
> > > TMT
>
> >
>
> > You are a pussy. Your friends are also tax sucking pussies!
>
> >
>
> > You need to take your goddamned ass and get the hell out of my country.- Hide quoted text -
>
> >
>
> > - Show quoted text -
>
>
>
> Try kissing my goddamned ass while I laugh, laugh, laugh.
>
>
>
> And thank you for your large donation to the Federal Government
>
> today...when we implement more taxes on guns/ammo it will be larger
>
> next year.
>
>
>
> TMT

I got something else for your ass you black piece of shit.