Ruby 3.4 リファレンスマニュアル

instance method Array#compact

compact -> Array[permalink][rdoc][edit]

自身から nil を取り除いた配列を生成して返します。

例
ary = [1, nil, 2, nil, 3, nil]
p ary.compact   # => [1, 2, 3]
p ary           # => [1, nil, 2, nil, 3, nil]

[SEE_ALSO] Array#compact!