311

Lets say I have an array

[0, 132, 432, 342, 234]

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

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352
  • 2
    'shift' is pop and 'unshift' is push. In which shift takes in the number of parameters to pop – Arun May 14 '13 at 10:19

11 Answers11

449

Use .drop(1).

This has the benefit of returning a new Array with the first element removed, as opposed to using .shift, which returns the removed element, not the Array with the first element removed.

NOTE: It does not affect/mutate the original Array.

a = [0,1,2,3]

a.drop(1)
# => [1, 2, 3] 

a
# => [0,1,2,3]

And, additionally, you can drop more than the first element:

[0,1,2,3].drop(2)
=> [2, 3]

[0,1,2,3].drop(3)
=> [3] 
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
scaryguy
  • 7,720
  • 3
  • 36
  • 52
  • 21
    Best answer since (a) evaluates to the trimmed array, and it works as expected on an empty array: `[].drop(1) => []` – fearless_fool Oct 21 '13 at 00:28
  • 4
    Also the best answer because OP said "what's the easiest way to get rid of the first element" – notaceo Feb 08 '14 at 06:21
  • 5
    This answer showcases that it is recommendable to scroll down even if the first and excepted answer has already more than 100 upvotes. Thank you. – maddin2code May 26 '15 at 16:33
  • 7
    +1 since this returns a modified array, whereas `shift` mutates the array in place and returns the shifted element (or `nil` if array was empty) – yuval Jan 15 '16 at 01:01
  • Folks, OP didn't specify whether or not they wanted the operation to mutate the original array. So this answer is no better than the accepted answer of #shift. In fact, #shift is a better match for OP's phrasing of "get rid of" IMO. A complete answer, however, would have provided both #shift and #drop and explained the difference. – ryanc Nov 18 '20 at 04:15
331

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
  • 34,892
  • 30
  • 114
  • 171
315

"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
  • 74,049
  • 16
  • 131
  • 175
  • 15
    Note this mutates the array and returns `nil` on an empty array. See `drop` for an alternative, as mentioned in the other answer. – Jay Oct 15 '15 at 21:37
140
[0, 132, 432, 342, 234][1..]
=> [132, 432, 342, 234]

So unlike shift or slice, this returns a new array, keeping the original array untouched (useful for one liners).

SylvainB
  • 394
  • 7
  • 16
vise
  • 12,713
  • 11
  • 52
  • 64
  • 14
    One gotcha to watch out for: if the array is empty it returns nil: `[][1..-1] => nil` and not `[]`. – Mohamad Jul 16 '13 at 01:26
  • 3
    Isn't `[1,2,3].shift` a one-liner? – thekingoftruth Oct 02 '13 at 23:50
  • 4
    @thekingoftruth: yes, but it evaluates to the element you threw away, not the rest of the array, so it takes another line. – fearless_fool Oct 21 '13 at 00:21
  • 4
    I like this answer because it's a one line _expression_ that you can use anywhere. To avoid the [] vs nil problem, you can do `arry[1..-1] || []`. But arry.drop(1) is even better. – fearless_fool Oct 21 '13 at 00:23
  • I'm going with this cuz it's simple, how would you get the "array minus what got shifted out", I can't see how that'd work... It's the main answer but it doesn't seem to answer the question since this next step isn't obvious to a newbie! – Louis Maddox Dec 01 '13 at 13:02
  • I like this approach since it's one-liner and does not modify original array – svelandiag Jun 30 '21 at 18:11
116

This is pretty neat:

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

As written in the comments, there's an advantage of not mutating the original list.

Community
  • 1
  • 1
hurikhan77
  • 5,881
  • 3
  • 32
  • 47
18

or a.delete_at 0

zzzhc
  • 395
  • 1
  • 7
18

Use shift method

array.shift(n) => Remove first n elements from array 
array.shift(1) => Remove first element

https://ruby-doc.org/core-2.2.0/Array.html#method-i-shift

Rahul Patel
  • 1,386
  • 1
  • 14
  • 16
15

You can use:

a.slice!(0)

slice! generalizes to any index or range.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
6

You can use Array.delete_at(0) method which will delete first element.

 x = [2,3,4,11,0]
 x.delete_at(0) unless x.empty? # [3,4,11,0]
lalit.sethi143
  • 146
  • 1
  • 5
  • I don't think `unless x.empty?` is necessary. It simply returns `nil` if the index is out of range. – webninja Jan 05 '18 at 17:41
6

You can use:

arr - [arr[0]]

or

arr - [arr.shift]

or simply

arr.shift(1)
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
Avijit Majhi
  • 508
  • 9
  • 15
2

You can use:

 a.delete(a[0])   
 a.delete_at 0

Both can work

sam
  • 372
  • 2
  • 12