# For loop iterator, next object

**URL:** <https://www.ruby-forum.com/t/for-loop-iterator-next-object/122138>\
**Category:** Ruby\
**Created:** [December 4, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/for-loop-iterator-next-object/122138 "2007-12-04T21:35:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 4, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/for-loop-iterator-next-object/122138/1 "2007-12-04T21:35:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 4, 2007, 9:43pm UTC](https://www.ruby-forum.com/t/for-loop-iterator-next-object/122138/2 "2007-12-04T21:43:11Z")

</div>

On Wed, 5 Dec 2007 05:35:01 +0900, “removed\_email\_address@domain.invalid”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 4, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/for-loop-iterator-next-object/122138/3 "2007-12-04T22:25:05Z")

</div>

On Wed, 5 Dec 2007 05:42:21 +0900, MenTaLguY [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 4, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/for-loop-iterator-next-object/122138/4 "2007-12-04T23:19:57Z")

</div>

On Dec 4, 2007 4:55 PM, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > if prior
> 
> 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 4, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/for-loop-iterator-next-object/122138/5 "2007-12-04T23:01:07Z")

</div>

On Dec 4, 4:19 pm, MenTaLguY [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > I want the second user.name to be the next object in the list. Is  
> > > 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 5, 2007, 3:45am UTC](https://www.ruby-forum.com/t/for-loop-iterator-next-object/122138/6 "2007-12-05T03:45:53Z")

</div>

On Dec 4, 2007 9:34 PM, Chris F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You can also use each\_with\_index:

I think using each\_with\_index that way will give behavior like  
each\_cons.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 8, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/for-loop-iterator-next-object/122138/7 "2007-12-08T12:01:01Z")

</div>

On 04.12.2007 23:18, Christian von Kleist wrote:

> > > > > I want the second user.name to be the next object in the list. Is  
> > > > > end
> 
> 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 5, 2007, 3:34am UTC](https://www.ruby-forum.com/t/for-loop-iterator-next-object/122138/8 "2007-12-05T03:34:44Z")

</div>

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
