How to map and remove nil values in Ruby

前端 未结 8 1596
野性不改
野性不改 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:28

    Definitely compact is the best approach for solving this task. However, we can achieve the same result just with a simple subtraction:

    [1, nil, 3, nil, nil] - [nil]
     => [1, 3]
    

提交回复
热议问题