ruby 从数组中删除一个元素 [英] ruby Remove one element from array
本文介绍了ruby 从数组中删除一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从数组中按值删除(仅一个)元素.
I want to remove (only one) element by value from my array.
示例:
x = [1,2,3,2]
x.remove(2)
result: x= [1,3]
但是,我想得到 [1,3,2]
.
谢谢
推荐答案
如@7urkm3n在评论中提到的,可以使用x.delete_at
删除第一次出现
As @7urkm3n mentioned in the comments, you can use x.delete_at
to delete the first occurance
x.delete_at(x.index 2)
<小时>
> x = [1,2,3,2]
=> [1, 2, 3, 2]
> x.delete_at(x.index 2)
=> 2
> x
=> [1, 3, 2]
这篇关于ruby 从数组中删除一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文