On Thu, 18 Jan 2001, Hugh Sasse Staff Elec Eng wrote: > What is the idiomatic Ruby way to go through all the elements of an array, > except the last one, gettting the index? > > my_array.each_index { > |i| > unless i == my_array.last { > # ... > } > } > > 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] > one doesn't want to fall off the end... > > Hugh > How about: (0...my_array.size - 1) .each do |i| ... end I don't really like the look of ranges in such situations (it always looks a bit hackish, though I'm not sure why), but at least it takes the test out of the block. David -- David Alan Black home: dblack / candle.superlink.net work: blackdav / shu.edu Web: http://pirate.shu.edu/~blackdav