From: Aleks Kissinger Date: 2006-07-26T23:43:37+09:00 Subject: Re: Truncate an array I'd thought about that too. This is bad for two reasons. 1. SOME_ARRAY is 'supposedly' a constant, in that ruby complains if you assign to it more than once. 2. We didn't really truncate the array, just made a new empty one. SOME_ARRAY = [1,2,3] a = SOME_ARRAY SOME_ARRAY = [] p a => [1, 2, 3] As opposed to something like: a = SOME_ARRAY SOME_ARRAY.length.times{SOME_ARRAY.shift} p a => [] On 7/26/06, Berger, Daniel wrote: > > -----Original Message----- > > From: Aleks Kissinger [mailto:aleks0@gmail.com] > > Sent: Wednesday, July 26, 2006 8:31 AM > > To: ruby-talk ML > > Subject: Truncate an array > > > > > > Is there a generally preferred way to truncate an array? > > I've used these two, but is there a better way? > > > > SOME_ARRAY = [1, 2, 3] > > > > 1. SOME_ARRAY.each_index{|i| SOME_ARRAY[i].nil}.compact! > > 2. SOME_ARRAY.slice!(1..0) > > > > => SOME_ARRAY == [] > > SOME_ARRAY = [] > > Regards, > > Dan > > > > This communication is the property of Qwest and may contain confidential or > privileged information. Unauthorized use of this communication is strictly > prohibited and may be unlawful. If you have received this communication > in error, please immediately notify the sender by reply e-mail and destroy > all copies of the communication and any attachments. > >