|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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/. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|