Lets say I have an array
[0, 132, 432, 342, 234]
What is the easiest way to get rid of the first element? (0)
Lets say I have an array
[0, 132, 432, 342, 234]
What is the easiest way to get rid of the first element? (0)
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)
[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).
This is pretty neat:
head, *tail = [1, 2, 3, 4, 5]
#==> head = 1, tail = [2, 3, 4, 5]