我的代码目前看起来像这样
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,我想我刚刚意识到我可以尝试切片!
是
numbers.pop(3)
Run Code Online (Sandbox Code Playgroud)
要么
numbers.shift(3)
Run Code Online (Sandbox Code Playgroud)
如果你想要这个另一面.
| 归档时间: |
|
| 查看次数: |
3847 次 |
| 最近记录: |