How to map and remove nil values in Ruby

前端 未结 8 1595
野性不改
野性不改 2020-12-07 07:51

I have a map which either changes a value or sets it to nil. I then want to remove the nil entries from the list. The list doesn\'t need to be kept.

Thi

8条回答
  •  情书的邮戳
    2020-12-07 08:34

    In your example:

    items.map! { |x| process_x url } # [1, 2, 3, 4, 5] => [1, nil, 3, nil, nil]
    

    it does not look like the values have changed other than being replaced with nil. If that is the case, then:

    items.select{|x| process_x url}
    

    will suffice.

提交回复
热议问题