gpt4 book ai didi

ruby - 多次弹出/移动 ruby​​ 数组

转载 作者:数据小太阳 更新时间:2023-10-29 07:13:54 25 4
gpt4 key购买 nike

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

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

def pop_three
pop = []
3.times { pop << numbers.pop }
return pop
end

有没有办法在一行中完成 pop_three 方法中的内容?

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

嗯...嗯,我想我刚刚意识到我可以试试 slice!

最佳答案

numbers.pop(3)

或者

numbers.shift(3)

如果你想要另一边。

关于ruby - 多次弹出/移动 ruby​​ 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7390646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com