# Previous value in array block

**URL:** <https://rubytalk.org/t/previous-value-in-array-block/54659>\
**Category:** ruby-talk\
**Created:** [29 July 2009 03:39 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659 "2009-07-29T03:39:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Lillywhite](https://avatars.discourse-cdn.com/v4/letter/j/b782af/32.png) [@Jason\_Lillywhite](https://rubytalk.org/u/Jason_Lillywhite)\
**Post date:** [29 July 2009 03:39 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/1 "2009-07-29T03:39:26Z")

</div>

Is this a good way to use a previous value in an array block?

I want to subtract the item(i) from item(i-1) of the array, a and return  
a new array of the results. Below is my attempt.

a = [1,3,3,2]  
prev\_item = 0  
b = []

a.each do |item|  
&nbsp;&nbsp;b \<\< item - prev\_item  
&nbsp;&nbsp;prev\_item = item  
end

puts b

=\> 1,2,0,-1

I'm thinking there must be a more elegant way of doing this.

thank you!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [29 July 2009 04:02 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/2 "2009-07-29T04:02:49Z")

</div>

> Is this a good way to use a previous value in an array block?
> 
> I want to subtract the item(i) from item(i-1) of the array, a and return  
> a new array of the results. Below is my attempt.
> 
> a = [1,3,3,2]  
> prev\_item = 0  
> b =
> 
> a.each do |item|  
> b \<\< item - prev\_item  
> prev\_item = item  
> end
> 
> puts b
> 
> =\> 1,2,0,-1
> 
> I'm thinking there must be a more elegant way of doing this.

to me, your code is elegant enough.  
not sure if making code shorter would make it more, so..

> a = [1,3,3,2]

=\> [1, 3, 3, 2]

> a.zip(a.dup.unshift(0)).map{|x,y| x-y}

=\> [1, 2, 0, -1]

kind regards -botp

> **···**
>
> On Wed, Jul 29, 2009 at 11:39 AM, Jason Lillywhite\<jason.lillywhite@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Deiva\_Rajasingam](https://avatars.discourse-cdn.com/v4/letter/d/47e85d/32.png) [@Deiva\_Rajasingam](https://rubytalk.org/u/Deiva_Rajasingam)\
**Post date:** [29 July 2009 04:10 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/3 "2009-07-29T04:10:13Z")

</div>

Jason Lillywhite wrote:

> a = [1,3,3,2]  
> prev\_item = 0  
> b =
> 
> a.each do |item|  
> &nbsp;&nbsp;b \<\< item - prev\_item  
> &nbsp;&nbsp;prev\_item = item  
> end
> 
> puts b
> 
> =\> 1,2,0,-1

Another way could be:

a = [1,3,3,2]  
b = [a[0]]

for i in 1...(a.length)  
&nbsp;&nbsp;b \<\< (a[i] - a[i-1])  
end

puts b

Not sure if that can be considered elegant though. 🙂

-DR

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Harry3](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@Harry3](https://rubytalk.org/u/Harry3)\
**Post date:** [29 July 2009 06:18 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/4 "2009-07-29T06:18:12Z")

</div>

> I want to subtract the item(i) from item(i-1) of the array, a and return  
> a new array of the results. Below is my attempt.

But, you are doing the opposite 🙂  
Anyway, based on your code,

FWIW, here is another one-liner.  
Elegant? I don't know.

b = Array.new(a.length){|i| a[i]-a[i-1]}[1..-1].unshift(a[0])

Harry

> **···**
>
> --  
> A Look into Japanese Ruby List in English
> 
> > **[Kakueki.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=kakueki.com)**
> >
> > Start using this domain right away. Straightforward domain shopping experience. Quick access to your domain.

---

<div class="post-metadata">

**Author:** ![steve9](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@steve9](https://rubytalk.org/u/steve9)\
**Post date:** [29 July 2009 08:05 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/5 "2009-07-29T08:05:10Z")

</div>

Jason Lillywhite wrote:

> Is this a good way to use a previous value in an array block?
> 
> I want to subtract the item(i) from item(i-1) of the array, a and return  
> a new array of the results. Below is my attempt.
> 
> a = [1,3,3,2]  
> prev\_item = 0  
> b =
> 
> a.each do |item|  
> &nbsp;&nbsp;b \<\< item - prev\_item  
> &nbsp;&nbsp;prev\_item = item  
> end
> 
> puts b
> 
> =\> 1,2,0,-1
> 
> I'm thinking there must be a more elegant way of doing this.
> 
> thank you!

I was recently reading through the facets docuementation and so thought I would develop a solution for the OP using the elementwise method that facets supplies. However while other facets methods from enumerable are available, elementwise does not seem to be.

C:\Documents and Settings\Administrator\>irb  
irb(main):001:0\> a=Array.new  
=\>   
irb(main):002:0\> a.respond\_to?("elementwise")  
=\> false  
irb(main):003:0\> a.respond\_to?("entropy")  
=\> false  
irb(main):004:0\> require "facets"  
=\> true  
irb(main):005:0\> a.respond\_to?("entropy")  
=\> true  
irb(main):006:0\> a.respond\_to?("elementwise")  
=\> false

I am using windows xp, one click Ruby 186-26 and facets 2.5.0.

Can some kind person point out my error?

Thanks

Steve

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [30 July 2009 06:10 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/6 "2009-07-30T06:10:14Z")

</div>

Never forget: we don't need no stinkin' loops!

x = 2,3,5,8,13  
p x.zip( [0] + x ).map{|a,b| a-b}

=\> [2, 1, 2, 3, 5]

> **···**
>
> On Jul 28, 10:39 pm, Jason Lillywhite \<jason.lillywh...@gmail.com\> wrote:
> 
> > Is this a good way to use a previous value in an array block?
> > 
> > I want to subtract the item(i) from item(i-1) of the array, a and return  
> > a new array of the results. Below is my attempt.
> > 
> > a = [1,3,3,2]  
> > prev\_item = 0  
> > b =
> > 
> > a.each do |item|  
> > &nbsp;&nbsp;b \<\< item - prev\_item  
> > &nbsp;&nbsp;prev\_item = item  
> > end
> > 
> > puts b
> > 
> > =\> 1,2,0,-1
> > 
> > I'm thinking there must be a more elegant way of doing this.
> > 
> > thank you!  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [30 July 2009 06:10 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/7 "2009-07-30T06:10:15Z")

</div>

Never forget: we don't need no stinkin' loops!

x = 2,3,5,8,13  
p x.zip( [0] + x ).map{|a,b| a-b}

=\> [2, 1, 2, 3, 5]

> **···**
>
> On Jul 28, 10:39 pm, Jason Lillywhite \<jason.lillywh...@gmail.com\> wrote:
> 
> > Is this a good way to use a previous value in an array block?
> > 
> > I want to subtract the item(i) from item(i-1) of the array, a and return  
> > a new array of the results. Below is my attempt.
> > 
> > a = [1,3,3,2]  
> > prev\_item = 0  
> > b =
> > 
> > a.each do |item|  
> > &nbsp;&nbsp;b \<\< item - prev\_item  
> > &nbsp;&nbsp;prev\_item = item  
> > end
> > 
> > puts b
> > 
> > =\> 1,2,0,-1
> > 
> > I'm thinking there must be a more elegant way of doing this.
> > 
> > thank you!  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [29 July 2009 07:45 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/8 "2009-07-29T07:45:01Z")

</div>

Another way:

irb(main):001:0\> a = [10,20,30,40]  
irb(main):014:0\> b =   
=\>   
irb(main):015:0\> a.each\_with\_index {|x,y| b \<\< (y \> 0? (x - a[y-1]): x)}  
=\> [10, 20, 30, 40]

irb(main):019:0\> b  
=\> [10, 10, 10, 10]

Jesus.

> **···**
>
> On Wed, Jul 29, 2009 at 8:18 AM, Harry Kakueki\<list.push@gmail.com\> wrote:
> 
> > > I want to subtract the item(i) from item(i-1) of the array, a and return  
> > > a new array of the results. Below is my attempt.
> > 
> > But, you are doing the opposite 🙂  
> > Anyway, based on your code,
> > 
> > FWIW, here is another one-liner.  
> > Elegant? I don't know.
> > 
> > b = Array.new(a.length){|i| a[i]-a[i-1]}[1..-1].unshift(a[0])

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [29 July 2009 08:50 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/9 "2009-07-29T08:50:31Z")

</div>

each\_cons seems natural here:

require 'enumerator'

a = [1, 3, 3, 2]  
b = a[0, 1]

a.each\_cons(2) { |x, y| b \<\< y - x }

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [29 July 2009 10:56 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/10 "2009-07-29T10:56:34Z")

</div>

Hi --

> **···**
>
> On Wed, 29 Jul 2009, Xavier Noria wrote:
> 
> > each\_cons seems natural here:
> > 
> > require 'enumerator'
> > 
> > a = [1, 3, 3, 2]  
> > b = a[0, 1]
> > 
> > a.each\_cons(2) { |x, y| b \<\< y - x }
> 
> Here's a 1.9 way, probably guilty of gross wasteage (by creating an  
> extra new array) but kind of cool:
> 
> [0,\*a].each\_cons(2).map {|x,y| y - x }
> 
> David
> 
> --  
> David A. Black / Ruby Power and Light, LLC / [http://www.rubypal.com](http://www.rubypal.com)  
> Q: What's the best way to get a really solid knowledge of Ruby?  
> A: Come to our Ruby training in Edison, New Jersey, September 14-17!  
> &nbsp;&nbsp;&nbsp;&nbsp;Instructors: David A. Black and Erik Kastner  
> &nbsp;&nbsp;&nbsp;&nbsp;More info and registration: [http://rubyurl.com/vmzN](http://rubyurl.com/vmzN)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [29 July 2009 12:11 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/11 "2009-07-29T12:11:45Z")

</div>

> each\_cons seems natural here:
> 
> require 'enumerator'
> 
> a = [1, 3, 3, 2]  
> b = a[0, 1]
> 
> a.each\_cons(2) { |x, y| b \<\< y - x }

and purely functional  
( [0] + a ).each\_cons( 2 ).map{ |x,y| y -x }  
or with Tom's map arity trick implemented  
( [0] + a ).map{ | x, y | y - x }

> **···**
>
> On Wed, Jul 29, 2009 at 10:50 AM, Xavier Noria\<fxn@hashref.com\> wrote:
> 
> > 
> 
> --  
> Toutes les grandes personnes ont d’abord été des enfants, mais peu  
> d’entre elles s’en souviennent.
> 
> All adults have been children first, but not many remember.
> 
> [Antoine de Saint-Exupéry]

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [29 July 2009 12:12 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/12 "2009-07-29T12:12:52Z")

</div>

A beauty, sorry did not see it before my post!  
Cheers  
Robert

> **···**
>
> On Wed, Jul 29, 2009 at 12:56 PM, David A. Black\<dblack@rubypal.com\> wrote:
> 
> > Hi --
> > 
> > On Wed, 29 Jul 2009, Xavier Noria wrote:
> > 
> > > each\_cons seems natural here:
> > > 
> > > require 'enumerator'
> > > 
> > > a = [1, 3, 3, 2]  
> > > b = a[0, 1]
> > > 
> > > a.each\_cons(2) { |x, y| b \<\< y - x }
> > 
> > Here's a 1.9 way, probably guilty of gross wasteage (by creating an  
> > extra new array) but kind of cool:
> > 
> > [0,\*a].each\_cons(2).map {|x,y| y - x }

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [29 July 2009 12:16 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/13 "2009-07-29T12:16:24Z")

</div>

Hi --

> **···**
>
> On Wed, 29 Jul 2009, Robert Dober wrote:
> 
> > On Wed, Jul 29, 2009 at 10:50 AM, Xavier Noria\<fxn@hashref.com\> wrote:
> > 
> > > each\_cons seems natural here:
> > > 
> > > require 'enumerator'
> > > 
> > > a = [1, 3, 3, 2]  
> > > b = a[0, 1]
> > > 
> > > a.each\_cons(2) { |x, y| b \<\< y - x }
> > 
> > and purely functional  
> > ( [0] + a ).each\_cons( 2 ).map{ |x,y| y -x }  
> > or with Tom's map arity trick implemented  
> > ( [0] + a ).map{ | x, y | y - x }
> 
> I don't understand that last one. Is this some kind of override of map  
> that turns it into each\_cons + map?
> 
> David
> 
> --  
> David A. Black / Ruby Power and Light, LLC / [http://www.rubypal.com](http://www.rubypal.com)  
> Q: What's the best way to get a really solid knowledge of Ruby?  
> A: Come to our Ruby training in Edison, New Jersey, September 14-17!  
> &nbsp;&nbsp;&nbsp;&nbsp;Instructors: David A. Black and Erik Kastner  
> &nbsp;&nbsp;&nbsp;&nbsp;More info and registration: [http://rubyurl.com/vmzN](http://rubyurl.com/vmzN)

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [29 July 2009 12:47 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/14 "2009-07-29T12:47:13Z")

</div>

very cool.  
thanks for the tip.

kind regards -botp

> **···**
>
> On Wed, Jul 29, 2009 at 8:11 PM, Robert Dober\<robert.dober@gmail.com\> wrote:
> 
> > or with Tom's map arity trick implemented  
> > ( [0] + a ).map{ | x, y | y - x }

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [29 July 2009 12:51 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/15 "2009-07-29T12:51:23Z")

</div>

Here's a lisp/scheme inspired approach using inject and with an array  
serving as a "cons cell" and first =\> car and last =\> cdr:

[1, 3, 3, 2].inject([0,]) { |memo, elem| [elem, memo.last \<\< (elem  
- memo.first)] }.last # =\> [1, 2, 0, -1]

> **···**
>
> On Wed, Jul 29, 2009 at 8:11 AM, Robert Dober\<robert.dober@gmail.com\> wrote:
> 
> > On Wed, Jul 29, 2009 at 10:50 AM, Xavier Noria\<fxn@hashref.com\> wrote:
> > 
> > > each\_cons seems natural here:
> > > 
> > > require 'enumerator'
> > > 
> > > a = [1, 3, 3, 2]  
> > > b = a[0, 1]
> > > 
> > > a.each\_cons(2) { |x, y| b \<\< y - x }
> > 
> > and purely functional  
> > ( [0] + a ).each\_cons( 2 ).map{ |x,y| y -x }
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [29 July 2009 12:21 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/16 "2009-07-29T12:21:40Z")

</div>

> Hi --
> 
> > > each\_cons seems natural here:
> > > 
> > > require 'enumerator'
> > > 
> > > a = [1, 3, 3, 2]  
> > > b = a[0, 1]
> > > 
> > > a.each\_cons(2) { |x, y| b \<\< y - x }
> > 
> > and purely functional  
> > ( [0] + a ).each\_cons( 2 ).map{ |x,y| y -x }  
> > or with Tom's map arity trick implemented  
> > ( [0] + a ).map{ | x, y | y - x }
> 
> I don't understand that last one. Is this some kind of override of map  
> that turns it into each\_cons + map?

IIRC it was like this

module Enumerable  
&nbsp;&nbsp;&nbsp;method\_alias :\_\_map\_\_, map  
&nbsp;&nbsp;&nbsp;def map &blk  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \_\_map\_\_(&blk) if blk.arity \< 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each\_cons( blk.arity).map(&blk)  
&nbsp;&nbsp;&nbsp;...

Robert

> **···**
>
> On Wed, Jul 29, 2009 at 2:16 PM, David A. Black\<dblack@rubypal.com\> wrote:
> 
> > On Wed, 29 Jul 2009, Robert Dober wrote:
> > 
> > > On Wed, Jul 29, 2009 at 10:50 AM, Xavier Noria\<fxn@hashref.com\> wrote:
> > 
> > David
> > 
> > --  
> > David A. Black / Ruby Power and Light, LLC / [http://www.rubypal.com](http://www.rubypal.com)  
> > Q: What's the best way to get a really solid knowledge of Ruby?  
> > A: Come to our Ruby training in Edison, New Jersey, September 14-17!  
> > Instructors: David A. Black and Erik Kastner  
> > More info and registration: [http://rubyurl.com/vmzN](http://rubyurl.com/vmzN)
> 
> --  
> Toutes les grandes personnes ont d’abord été des enfants, mais peu  
> d’entre elles s’en souviennent.
> 
> All adults have been children first, but not many remember.
> 
> [Antoine de Saint-Exupéry]

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [29 July 2009 12:31 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/17 "2009-07-29T12:31:46Z")

</div>

Hi --

> **···**
>
> On Wed, 29 Jul 2009, Robert Dober wrote:
> 
> > On Wed, Jul 29, 2009 at 2:16 PM, David A. Black\<dblack@rubypal.com\> wrote:
> > 
> > > Hi --
> > > 
> > > On Wed, 29 Jul 2009, Robert Dober wrote:
> > > 
> > > > On Wed, Jul 29, 2009 at 10:50 AM, Xavier Noria\<fxn@hashref.com\> wrote:
> > > > 
> > > > > each\_cons seems natural here:
> > > > > 
> > > > > require 'enumerator'
> > > > > 
> > > > > a = [1, 3, 3, 2]  
> > > > > b = a[0, 1]
> > > > > 
> > > > > a.each\_cons(2) { |x, y| b \<\< y - x }
> > > > 
> > > > and purely functional  
> > > > ( [0] + a ).each\_cons( 2 ).map{ |x,y| y -x }  
> > > > or with Tom's map arity trick implemented  
> > > > ( [0] + a ).map{ | x, y | y - x }
> > > 
> > > I don't understand that last one. Is this some kind of override of map  
> > > that turns it into each\_cons + map?
> > 
> > IIRC it was like this
> > 
> > module Enumerable  
> > &nbsp;&nbsp;method\_alias :\_\_map\_\_, map  
> > &nbsp;&nbsp;def map &blk  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \_\_map\_\_(&blk) if blk.arity \< 2  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each\_cons( blk.arity).map(&blk)  
> > &nbsp;&nbsp;...
> 
> I confess I don't see the point, but thanks for clarifying.
> 
> David
> 
> --  
> David A. Black / Ruby Power and Light, LLC / [http://www.rubypal.com](http://www.rubypal.com)  
> Q: What's the best way to get a really solid knowledge of Ruby?  
> A: Come to our Ruby training in Edison, New Jersey, September 14-17!  
> &nbsp;&nbsp;&nbsp;&nbsp;Instructors: David A. Black and Erik Kastner  
> &nbsp;&nbsp;&nbsp;&nbsp;More info and registration: [http://rubyurl.com/vmzN](http://rubyurl.com/vmzN)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [29 July 2009 12:41 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/18 "2009-07-29T12:41:12Z")

</div>

It is cheating, bending Ruby to the needs of the problem. We even had  
a discussion if it should use each\_cons or each\_slice below the hood.

But the point might be, that it might make sense to have things like  
map\_cons or map\_slice  
I can only think about Ruby code, writing it, sorry.  
Gotta look into Facets for that.

Cheers  
Robert

> **···**
>
> On Wed, Jul 29, 2009 at 2:31 PM, David A. Black\<dblack@rubypal.com\> wrote:
> 
> > I confess I don't see the point, but thanks for clarifying.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [29 July 2009 13:20 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/19 "2009-07-29T13:20:05Z")

</div>

Hi --

> **···**
>
> On Wed, 29 Jul 2009, Robert Dober wrote:
> 
> > On Wed, Jul 29, 2009 at 2:31 PM, David A. Black\<dblack@rubypal.com\> wrote:
> > 
> > > I confess I don't see the point, but thanks for clarifying.
> > 
> > It is cheating, bending Ruby to the needs of the problem. We even had  
> > a discussion if it should use each\_cons or each\_slice below the hood.
> > 
> > But the point might be, that it might make sense to have things like  
> > map\_cons or map\_slice
> 
> In 1.9 you get: enumerable.each\_cons(x).map ... which has much the  
> same effect. I don't like the idea of map having a stealth each\_cons  
> in it. Also, defining it for Enumerable won't affect arrays (at least  
> in MRI), because Array overries map.
> 
> David
> 
> --  
> David A. Black / Ruby Power and Light, LLC / [http://www.rubypal.com](http://www.rubypal.com)  
> Q: What's the best way to get a really solid knowledge of Ruby?  
> A: Come to our Ruby training in Edison, New Jersey, September 14-17!  
> &nbsp;&nbsp;&nbsp;&nbsp;Instructors: David A. Black and Erik Kastner  
> &nbsp;&nbsp;&nbsp;&nbsp;More info and registration: [http://rubyurl.com/vmzN](http://rubyurl.com/vmzN)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [29 July 2009 14:52 UTC](https://rubytalk.org/t/previous-value-in-array-block/54659/20 "2009-07-29T14:52:48Z")

</div>

yes we should not confuse, playing around and changing the language. I  
have sometimes the feeling that people get nervous when I do this  
"cheating" stuff, but if I meant this to be in Ruby I would say so and  
probably at Ruby core. No it is just for the fun and elegance.  
R.

> **···**
>
> On Wed, Jul 29, 2009 at 3:20 PM, David A. Black\<dblack@rubypal.com\> wrote:
> 
> > Hi --
> > 
> > On Wed, 29 Jul 2009, Robert Dober wrote:
> > 
> > > On Wed, Jul 29, 2009 at 2:31 PM, David A. Black\<dblack@rubypal.com\> wrote:
> > > 
> > > > I confess I don't see the point, but thanks for clarifying.
> > > 
> > > It is cheating, bending Ruby to the needs of the problem. We even had  
> > > a discussion if it should use each\_cons or each\_slice below the hood.
> > > 
> > > But the point might be, that it might make sense to have things like  
> > > map\_cons or map\_slice
> > 
> > In 1.9 you get: enumerable.each\_cons(x).map ... which has much the  
> > same effect. I don't like the idea of map having a stealth each\_cons  
> > in it. Also, defining it for Enumerable won't affect arrays (at least  
> > in MRI), because Array overries map.
