views:

160

answers:

6

Lets say I have an array

[0, 132, 432, 342, 234]

What is the easiest way to get rid of the first element? (0)

+14  A: 

"pop"ing the first element of an Array is called "shift" ("unshift" being the operation of adding one element in front of the array).

Sjoerd
+1  A: 

You can use:

a.slice!(0)

slice! generalizes to any index or range.

Matthew Flaschen
+11  A: 

Use the shift method on array

>> x = [4,5,6]
=> [4, 5, 6]                                                            
>> x.shift 
=> 4
>> x                                                                    
=> [5, 6] 

If you want to remove n starting elements you can use x.shift(n)

Bragboy
+2  A: 
[0, 132, 432, 342, 234][1..-1]
=> [132, 432, 342, 234]

So unlike shift or slice this returns the modified array (useful for one liners).

vise
+1  A: 

or a.delete_at 0

zzzhc
+1  A: 

This is pretty neat:

head, *tail = [1, 2, 3, 4, 5]
#==> head = 1, tail = [2, 3, 4, 5]
hurikhan77