From: Robert Klemme Date: 2009-01-29T17:05:49+09:00 Subject: Re: Real life use of each_cons? 2009/1/29 Mischa Fierer : > Hello, > > I've been going through the enumerable/enumerator methods that I'm > unfamiliar with and came across each_cons. Apart from a post by a guy > who wasn't sure where it was even defined, I have only come across one > use of it in the wild: > > (From Prawn) > > def polygon(*points) > move_to points[0] > (points << points[0]).each_cons(2) do |p1,p2| > line_to(*p2) > end > end > > > Does anyone have any ideas on other cases where each_cons would be > useful? Or perhaps insight into why it's in Ruby? You can find some uses in this mailing list's archive: http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb?key=each_cons&cginame=namazu.rb&submit=Search&dbname=ruby-talk&max=50&whence=0 Useful applications are those where you need a moving window over a collection of data, for example when calculating moving averages (smoothing a plot) or distances between adjacent values. Kind regards robert -- remember.guy do |as, often| as.you_can - without end