# Each with previous?

**URL:** <https://rubytalk.org/t/each-with-previous/43016>\
**Category:** ruby-talk\
**Created:** [14 December 2007 15:28 UTC](https://rubytalk.org/t/each-with-previous/43016 "2007-12-14T15:28:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jari\_Williamsson](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@Jari\_Williamsson](https://rubytalk.org/u/Jari_Williamsson)\
**Post date:** [14 December 2007 15:28 UTC](https://rubytalk.org/t/each-with-previous/43016/1 "2007-12-14T15:28:51Z")

</div>

I'm going through lots of data where the result of the current is affected by the previous element. So what I would need is an  
"each\_with\_previous {|current, prev|}"  
...to make it a bit more readable. Is there any built-in Ruby method that I might have overlooked, or should I build my own?

Best regards,

Jari Williamsson

---

<div class="post-metadata">

**Author:** ![Eivind\_Eklund](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Eivind\_Eklund](https://rubytalk.org/u/Eivind_Eklund)\
**Post date:** [14 December 2007 15:39 UTC](https://rubytalk.org/t/each-with-previous/43016/2 "2007-12-14T15:39:06Z")

</div>

Enumerable.each\_cons(2) { |current, prev| ... }

Eivind.

> **···**
>
> On Dec 14, 2007 4:28 PM, Jari Williamsson \<jari.williamsson@mailbox.swipnet.se\> wrote:
> 
> > I'm going through lots of data where the result of the current is  
> > affected by the previous element. So what I would need is an  
> > "each\_with\_previous {|current, prev|}"  
> > ...to make it a bit more readable. Is there any built-in Ruby method  
> > that I might have overlooked, or should I build my own?

---

<div class="post-metadata">

**Author:** ![Gregory\_Seidman](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@Gregory\_Seidman](https://rubytalk.org/u/Gregory_Seidman)\
**Post date:** [14 December 2007 15:39 UTC](https://rubytalk.org/t/each-with-previous/43016/3 "2007-12-14T15:39:36Z")

</div>

> I'm going through lots of data where the result of the current is affected  
> by the previous element. So what I would need is an  
> "each\_with\_previous {|current, prev|}"  
> ...to make it a bit more readable. Is there any built-in Ruby method that I  
> might have overlooked, or should I build my own?

You can fake it pretty simply with inject. For example:

> > [1,2,3,4,5].inject { |prev,cur| puts "#{prev}, #{cur}"; cur }

1, 2  
2, 3  
3, 4  
4, 5

Note that you do need the block to "return" (i.e. evaluate to) the current  
element so that it gets passed into the next iteration.

> Best regards,  
> Jari Williamsson

--Greg

> **···**
>
> On Sat, Dec 15, 2007 at 12:28:51AM +0900, Jari Williamsson wrote:

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [14 December 2007 15:44 UTC](https://rubytalk.org/t/each-with-previous/43016/4 "2007-12-14T15:44:49Z")

</div>

require 'enumerator'

[first, second, third].each\_slice(2) do |prev,curr|  
&nbsp;&nbsp;&nbsp;# do stuff  
end

Except that you don't get a [nil,first] pair to start, you get [first,second].

You could also do something like:

[first, second, third].inject(nil) do |prev, curr|  
&nbsp;&nbsp;&nbsp;# do stuff  
&nbsp;&nbsp;&nbsp;curr  
end

And curr is assigned to prev in the next iteration and the first iteration gets [nil,first]

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Dec 14, 2007, at 10:28 AM, Jari Williamsson wrote:
> 
> > I'm going through lots of data where the result of the current is affected by the previous element. So what I would need is an  
> > "each\_with\_previous {|current, prev|}"  
> > ...to make it a bit more readable. Is there any built-in Ruby method that I might have overlooked, or should I build my own?
> > 
> > Best regards,
> > 
> > Jari Williamsson

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [14 December 2007 16:15 UTC](https://rubytalk.org/t/each-with-previous/43016/5 "2007-12-14T16:15:03Z")

</div>

Take your pick:

irb(main):001:0\> a = %w| a b c d e f g h |  
=\> ["a", "b", "c", "d", "e", "f", "g", "h"]

irb(main):002:0\> require 'enumerator'

irb(main):003:0\> a.each\_cons(2){ |x,y| puts "#{x}-#{y}" }  
a-b  
b-c  
c-d  
d-e  
e-f  
f-g  
g-h

irb(main):004:0\> a.each\_slice(2){ |x,y| puts "#{x}-#{y}" }  
a-b  
c-d  
e-f  
g-h

> **···**
>
> On Dec 14, 8:28 am, Jari Williamsson \<jari.williams...@mailbox.swipnet.se\> wrote:
> 
> > I'm going through lots of data where the result of the current is  
> > affected by the previous element. So what I would need is an  
> > "each\_with\_previous {|current, prev|}"  
> > ..to make it a bit more readable. Is there any built-in Ruby method  
> > that I might have overlooked, or should I build my own?

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [14 December 2007 18:05 UTC](https://rubytalk.org/t/each-with-previous/43016/6 "2007-12-14T18:05:01Z")

</div>

There is a subtlety: with your code there will be no previous for the first element. Depending on what the OP needs you can as well do

irb(main):001:0\> (1..5).inject(nil) {|prev,curr| p [prev,curr];curr}  
[nil, 1]  
[1, 2]  
[2, 3]  
[3, 4]  
[4, 5]  
=\> 5

Jari, what kind of calculation do you do?

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 14.12.2007 16:39, Gregory Seidman wrote:
> 
> > On Sat, Dec 15, 2007 at 12:28:51AM +0900, Jari Williamsson wrote:
> > 
> > > I'm going through lots of data where the result of the current is affected by the previous element. So what I would need is an  
> > > "each\_with\_previous {|current, prev|}"  
> > > ...to make it a bit more readable. Is there any built-in Ruby method that I might have overlooked, or should I build my own?
> > 
> > You can fake it pretty simply with inject. For example:
> > 
> > > > [1,2,3,4,5].inject { |prev,cur| puts "#{prev}, #{cur}"; cur }
> > 
> > 1, 2  
> > 2, 3  
> > 3, 4  
> > 4, 5
> > 
> > Note that you do need the block to "return" (i.e. evaluate to) the current  
> > element so that it gets passed into the next iteration.

---

<div class="post-metadata">

**Author:** ![Jari\_Williamsson](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@Jari\_Williamsson](https://rubytalk.org/u/Jari_Williamsson)\
**Post date:** [14 December 2007 23:57 UTC](https://rubytalk.org/t/each-with-previous/43016/7 "2007-12-14T23:57:37Z")

</div>

Robert Klemme wrote:

> There is a subtlety: with your code there will be no previous for the first element. Depending on what the OP needs you can as well do
> 
> irb(main):001:0\> (1..5).inject(nil) {|prev,curr| p [prev,curr];curr}  
> [nil, 1]  
> [1, 2]  
> [2, 3]  
> [3, 4]  
> [4, 5]  
> =\> 5
> 
> Jari, what kind of calculation do you do?

I'll use different approaches for different situations. Starting inject with nil or first item will both become handy. Thanks!

The tasks are for analyzing musical data. The current note is most often dependent of previous context.

Best regards,

Jari Williamsson
