[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Truncate an array

Berger, Daniel

7/26/2006 2:36:00 PM

> -----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.

1 Answer

Aleks Kissinger

7/26/2006 2:44:00 PM

0

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 <Daniel.Berger@qwest.com> 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.
>
>