Ruby: What is the easiest way to remove the first element from an array?

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


Lets say I have an array

[0, 132, 432, 342, 234]

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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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] 

Solution 2 - Ruby on-Rails

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)

Solution 3 - Ruby on-Rails

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

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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.

Solution 6 - Ruby on-Rails

or a.delete_at 0

Solution 7 - Ruby on-Rails

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

Solution 8 - Ruby on-Rails

You can use:

a.slice!(0)

slice! generalizes to any index or range.

Solution 9 - Ruby on-Rails

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]

Solution 10 - Ruby on-Rails

You can use:

arr - [arr[0]]

or

arr - [arr.shift]

or simply

arr.shift(1)

Solution 11 - Ruby on-Rails

You can use:

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

Both can work

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNullVoxPopuliView Question on Stackoverflow
Solution 1 - Ruby on-RailsscaryguyView Answer on Stackoverflow
Solution 2 - Ruby on-RailsbragboyView Answer on Stackoverflow
Solution 3 - Ruby on-RailsSjoerdView Answer on Stackoverflow
Solution 4 - Ruby on-RailsviseView Answer on Stackoverflow
Solution 5 - Ruby on-Railshurikhan77View Answer on Stackoverflow
Solution 6 - Ruby on-RailszzzhcView Answer on Stackoverflow
Solution 7 - Ruby on-RailsRahul PatelView Answer on Stackoverflow
Solution 8 - Ruby on-RailsMatthew FlaschenView Answer on Stackoverflow
Solution 9 - Ruby on-Railslalit.sethi143View Answer on Stackoverflow
Solution 10 - Ruby on-RailsAvijit MajhiView Answer on Stackoverflow
Solution 11 - Ruby on-RailssamView Answer on Stackoverflow