PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > for loop iterator, next object
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
for loop iterator, next object

Réponse
 
LinkBack Outils de la discussion
Vieux 04/12/2007, 20h31   #1
lpgauth@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut for loop iterator, next object

Assume I have:

for user in @Users
put user.name
put user.name #I want this to be next object in @Users
end

I want the second user.name to be the next object in the list. Is
there a way to do this?

Thanks in advance,
LP

  Réponse avec citation
Vieux 04/12/2007, 20h42   #2
MenTaLguY
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: for loop iterator, next object

On Wed, 5 Dec 2007 05:35:01 +0900, "lpgauth@gmail.com" <lpgauth@gmail.com> wrote:
> Assume I have:
>
> for user in @Users
> put user.name
> put user.name #I want this to be next object in @Users
> end
>
> I want the second user.name to be the next object in the list. Is
> there a way to do this?


Of course. But what do you want to happen when you're at the end
of the list and there is no longer a next object?

-mental


  Réponse avec citation
Vieux 04/12/2007, 21h19   #3
MenTaLguY
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: for loop iterator, next object

On Wed, 5 Dec 2007 05:42:21 +0900, MenTaLguY <mental@rydia.net> wrote:
> On Wed, 5 Dec 2007 05:35:01 +0900, "lpgauth@gmail.com" <lpgauth@gmail.com>
> wrote:
>> Assume I have:
>>
>> for user in @Users
>> put user.name
>> put user.name #I want this to be next object in @Users
>> end
>>
>> I want the second user.name to be the next object in the list. Is
>> there a way to do this?

>
> Of course. But what do you want to happen when you're at the end
> of the list and there is no longer a next object?


That is, if you only care about pairs of users, then you can do this:

prior = nil
for user in @Users
if prior
put prior.name
put user.name
end
prior = user
end

Obviously that won't print anything if there is only one entry in
@Users, though.

-mental


  Réponse avec citation
Vieux 04/12/2007, 21h53   #4
lpgauth@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: for loop iterator, next object

On Dec 4, 4:19 pm, MenTaLguY <men...@rydia.net> wrote:
> On Wed, 5 Dec 2007 05:42:21 +0900, MenTaLguY <men...@rydia.net> wrote:
> > On Wed, 5 Dec 2007 05:35:01 +0900, "lpga...@gmail.com" <lpga...@gmail.com>
> > wrote:
> >> Assume I have:

>
> >> for user in @Users
> >> put user.name
> >> put user.name #I want this to be next object in @Users
> >> end

>
> >> I want the second user.name to be the next object in the list. Is
> >> there a way to do this?

>
> > Of course. But what do you want to happen when you're at the end
> > of the list and there is no longer a next object?

>
> That is, if you only care about pairs of users, then you can do this:
>
> prior = nil
> for user in @Users
> if prior
> put prior.name
> put user.name
> end
> prior = user
> end
>
> Obviously that won't print anything if there is only one entry in
> @Users, though.
>
> -mental


I figured it out... You can use in_group_of
  Réponse avec citation
Vieux 04/12/2007, 22h18   #5
Christian von Kleist
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: for loop iterator, next object

On Dec 4, 2007 4:55 PM, lpgauth@gmail.com <lpgauth@gmail.com> wrote:
> On Dec 4, 4:19 pm, MenTaLguY <men...@rydia.net> wrote:
> > On Wed, 5 Dec 2007 05:42:21 +0900, MenTaLguY <men...@rydia.net> wrote:
> > > On Wed, 5 Dec 2007 05:35:01 +0900, "lpga...@gmail.com" <lpga...@gmail.com>
> > > wrote:
> > >> Assume I have:

> >
> > >> for user in @Users
> > >> put user.name
> > >> put user.name #I want this to be next object in @Users
> > >> end

> >
> > >> I want the second user.name to be the next object in the list. Is
> > >> there a way to do this?

> >
> > > Of course. But what do you want to happen when you're at the end
> > > of the list and there is no longer a next object?

> >
> > That is, if you only care about pairs of users, then you can do this:
> >
> > prior = nil
> > for user in @Users
> > if prior
> > put prior.name
> > put user.name
> > end
> > prior = user
> > end
> >
> > Obviously that won't print anything if there is only one entry in
> > @Users, though.
> >
> > -mental

>
> I figured it out... You can use in_group_of
>
>



Cool! That's from Rails (not part of Ruby), but there's also each_slice(n):

require 'enumerable'

a = [1, 2, 3, 4]

a.each_slice(2) {|slice| puts slice[0] + slice[1]}

=>

3
7

  Réponse avec citation
Vieux 05/12/2007, 02h34   #6
Chris Fisher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: for loop iterator, next object

Christian von Kleist wrote:
> Cool! That's from Rails (not part of Ruby), but there's also
> each_slice(n):
>
> require 'enumerable'
>
> a = [1, 2, 3, 4]
>
> a.each_slice(2) {|slice| puts slice[0] + slice[1]}


You can also use each_with_index:

users.each_with_index do |user, index|
puts user
if index < users.length
puts users[index + 1]
end
end
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 05/12/2007, 02h44   #7
Christian von Kleist
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: for loop iterator, next object

On Dec 4, 2007 9:34 PM, Chris Fisher <fourfifty@gmail.com> wrote:
> Christian von Kleist wrote:
> > Cool! That's from Rails (not part of Ruby), but there's also
> > each_slice(n):
> >
> > require 'enumerable'
> >
> > a = [1, 2, 3, 4]
> >
> > a.each_slice(2) {|slice| puts slice[0] + slice[1]}

>
> You can also use each_with_index:
>
> users.each_with_index do |user, index|
> puts user
> if index < users.length
> puts users[index + 1]
> end
> end
> --
> Posted via http://www.ruby-forum.com/.
>
>


I think using each_with_index that way will give behavior like each_cons.

  Réponse avec citation
Vieux 08/12/2007, 10h57   #8
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: for loop iterator, next object

On 04.12.2007 23:18, Christian von Kleist wrote:
> On Dec 4, 2007 4:55 PM, lpgauth@gmail.com <lpgauth@gmail.com> wrote:
>> On Dec 4, 4:19 pm, MenTaLguY <men...@rydia.net> wrote:
>>> On Wed, 5 Dec 2007 05:42:21 +0900, MenTaLguY <men...@rydia.net> wrote:
>>>> On Wed, 5 Dec 2007 05:35:01 +0900, "lpga...@gmail.com" <lpga...@gmail.com>
>>>> wrote:
>>>>> Assume I have:
>>>>> for user in @Users
>>>>> put user.name
>>>>> put user.name #I want this to be next object in @Users
>>>>> end
>>>>> I want the second user.name to be the next object in the list. Is
>>>>> there a way to do this?
>>>> Of course. But what do you want to happen when you're at the end
>>>> of the list and there is no longer a next object?
>>> That is, if you only care about pairs of users, then you can do this:
>>>
>>> prior = nil
>>> for user in @Users
>>> if prior
>>> put prior.name
>>> put user.name
>>> end
>>> prior = user
>>> end
>>>
>>> Obviously that won't print anything if there is only one entry in
>>> @Users, though.
>>>
>>> -mental

>> I figured it out... You can use in_group_of
>>
>>

>
>
> Cool! That's from Rails (not part of Ruby), but there's also each_slice(n):
>
> require 'enumerable'
>
> a = [1, 2, 3, 4]
>
> a.each_slice(2) {|slice| puts slice[0] + slice[1]}


I'd probably rather use #each_cons:

irb(main):001:0> %w{a b c}.each_cons(2) {|*a| p a}
[["a", "b"]]
[["b", "c"]]
=> nil

Cheers

robert
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h04.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17925 seconds with 16 queries