# Re: Truncate an array

**URL:** <https://www.ruby-forum.com/t/re-truncate-an-array/100002>\
**Category:** Ruby\
**Created:** [July 26, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/re-truncate-an-array/100002 "2006-07-26T16:38:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 26, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/re-truncate-an-array/100002/1 "2006-07-26T16:38:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 26, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/re-truncate-an-array/100002/2 "2006-07-26T16:45:13Z")

</div>

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  
=\> []
