How can I delete one element from an array by value

前端 未结 15 1040
独厮守ぢ
独厮守ぢ 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:19

    I like the -=[4] way mentioned in other answers to delete the elements whose value are 4.

    But there is this way:

    irb(main):419:0> [2,4,6,3,8,6].delete_if{|i|i==6}
    => [2, 4, 3, 8]
    irb(main):420:0>
    

    mentioned somewhere in "Basic Array Operations", after it mentions the map function.

提交回复
热议问题