From: r2d2@... (Niklas Frykholm) Date: 2001-01-18T22:10:02+09:00 Subject: [ruby-talk:9504] Re: array.each_index_but_last ? >What is the idiomatic Ruby way to go through all the elements of an array, >except the last one, gettting the index? >... >seems less than neat. > >Reason I want to do this -- If one is producing a difference table from an >array such that: > d[i] = a[i+1] - a[i] If you want to do things like this a lot you could create iterators for looping over the array elements pairwise class Array def each_pair (0...size-1).each {|i| yield(self[i],self[i+1])} end def each_pair_with_index (0...size-1).each {|i| yield(self[i],self[i+1],i)} end end Now you could just do d = [] a.each_pair {|i, j| d.push(j-i)} // Niklas