From: David Alan Black Date: 2004-05-08T21:54:02+09:00 Subject: Re: How to get the last 5 elements of an array? Hi -- Gavin Sinclair writes: > On Saturday, May 8, 2004, 10:24:02 PM, Xavier wrote: > > > But [1,2][-5,5] =>> nil > > and Gavin wants [1,2] instead of nil > > > How about > > > def last5(arr) > > if arr.length<5 then arr else arr[-5,5] end > > end > > > Right. Now should that be necessary? Grabbing the last 5 elements of > an array is the kind of thing that should be easy and obvious in Ruby. > > So I propose that Array be changed so that > > def last5(arr) > arr[-5..-1] > end > > meets the requirements I gave earlier. That is > > last5 [] # -> [] > last5 [1] # -> [1] > last5 [1,2] # -> [1,2] > last5 [1,2,3,4,5,6,7] # -> [3,4,5,6,7] > last5 nil # -> error > > Any thoughts before I raise an RCR? Yes. I don't think it makes sense for this: arr[-5..-1].include?(arr[-3]) to be false, since this: (-5..-1).include?(-3) is true. But none of this matters, because Array#last takes an argument :-) irb(main):008:0> [1,2,3].last(2) => [2, 3] David -- David A. Black dblack@wobblini.net