How can I delete one element from an array by value

前端 未结 15 1054
独厮守ぢ
独厮守ぢ 2020-12-04 05:16

I have an array of elements in Ruby

[2,4,6,3,8]

I need to remove elements with value 3 for example

How do I do that?<

15条回答
  •  佛祖请我去吃肉
    2020-12-04 06:12

    If you also want to make this deletion operation chainable, so you can delete some item and keep on chaining operations on the resulting array, use tap:

    [2, 4, 6, 3, 8].tap { |ary| ary.delete(3) }.count #=> 4
    

提交回复
热议问题