多次弹出/移动ruby数组

Bra*_*and 5 ruby arrays

我的代码目前看起来像这样

numbers = [1, 2, 3, 4, 5]

def pop_three
  pop = []
  3.times { pop << numbers.pop }
  return pop
end
Run Code Online (Sandbox Code Playgroud)

有什么pop_three方法可以在一行中执行方法内部的操作吗?

我基本上想要做一些事情,numbers.slice(0, 3)但删除切片中的数组项.

嗯... hrmmm,我想我刚刚意识到我可以尝试切片!

mb1*_*b14 8

numbers.pop(3)
Run Code Online (Sandbox Code Playgroud)

要么

numbers.shift(3)
Run Code Online (Sandbox Code Playgroud)

如果你想要这个另一面.

  • @brand我没看过doc.刚试过 有时Ruby会按预期行事;-) (2认同)