假设我有一个数组

[0, 132, 432, 342, 234]

去掉第一个元素最简单的方法是什么?(0)


当前回答

或者a.delete_at 0

其他回答

“弹出”数组的第一个元素称为“shift”(“unshift”) 是添加一个元素的操作 在数组前面)。

使用shift法

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

这很简洁:

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

正如评论中所写的,不改变原始列表的优点。

你可以使用:

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

两者都可以

你可以使用:

a.slice!(0)

片!泛化到任何索引或范围。