How to remove all elements that satisfy a condition in array in Ruby?

后端 未结 4 1597
我寻月下人不归
我寻月下人不归 2021-02-06 20:51

How can I implement this in Ruby? Is there any one line of code technique? Let\'s say I want to get rid of all the elements which are less than 3 of an integer array.

4条回答
  •  孤城傲影
    2021-02-06 21:34

    Probably worth pointing out that

    array.reject! {|x| x < 3}
    

    and

    array.delete_if {|x| x < 3}
    

    Are the same, but

    array.reject {|x| x < 3}
    

    Will still return the same result, but not change the "array".

提交回复
热议问题