ChatGPT解决这个技术问题 Extra ChatGPT

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

Lets say I have an array

[0, 132, 432, 342, 234]

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

'shift' is pop and 'unshift' is push. In which shift takes in the number of parameters to pop

J
Joshua Pinter

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] 

Best answer since (a) evaluates to the trimmed array, and it works as expected on an empty array: [].drop(1) => []
Also the best answer because OP said "what's the easiest way to get rid of the first element"
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.
+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)
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.
b
bragboy

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)


the explanation should be merged into the correct answer since the content is the same
S
Sjoerd

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


Note this mutates the array and returns nil on an empty array. See drop for an alternative, as mentioned in the other answer.
S
SylvainB
[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).


One gotcha to watch out for: if the array is empty it returns nil: [][1..-1] => nil and not [].
Isn't [1,2,3].shift a one-liner?
@thekingoftruth: yes, but it evaluates to the element you threw away, not the rest of the array, so it takes another line.
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.
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!
C
Community

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.


very clever, and works perfectly. Once you do this, you'll have both variables "head" and "tail" at your disposal.
This reminds me a lot of lisp. (let ((head (car mylist)) (tail (cdr mylist)) ...)
@hurikhan77 Nice! I've been meaning to give it a try. Knowing that Darcs is written in Haskell helps pique my interest.
This version also has the advantage of not mutating the original list
useful when extracting info without an aditional step header, *properties = CSV.read(file_path,encoding: 'ISO-8859-1')
z
zzzhc

or a.delete_at 0


R
Rahul Patel

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


M
Matthew Flaschen

You can use:

a.slice!(0)

slice! generalizes to any index or range.


l
lalit.sethi143

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]

I don't think unless x.empty? is necessary. It simply returns nil if the index is out of range.
d
de-russification

You can use:

arr - [arr[0]]

or

arr - [arr.shift]

or simply

arr.shift(1)

arr - [arr[0]] this would fail for a = [1,2,3,4,5,6,7,8,1,2,3,4], because 1 is repeated.
s
sam

You can use:

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

Both can work