From: Gavin Sinclair Date: 2004-05-08T21:37:32+09:00 Subject: Re: How to get the last 5 elements of an array? 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? Cheers, Gavin