From: Robert Dober Date: 2009-07-29T21:21:40+09:00 Subject: Re: previous value in array block On Wed, Jul 29, 2009 at 2:16 PM, David A. Black wrote: > Hi -- > > On Wed, 29 Jul 2009, Robert Dober wrote: > >> On Wed, Jul 29, 2009 at 10:50 AM, 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 } >> >> 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 method_alias :__map__, map def map &blk return __map__(&blk) if blk.arity < 2 each_cons( blk.arity).map(&blk) ... Robert > > > David > > -- > David A. Black / Ruby Power and Light, LLC / 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 > > -- 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]