このマニュアルは既にメンテナンスが終了したバージョンの Ruby を対象としています。 最新版のマニュアルへ

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

instance method Array#compact!

compact! -> self | nil[permalink][rdoc][edit]

自身から破壊的に nil を取り除き、変更が行われた場合は self を、そうでなければ nil を返します。

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

[SEE_ALSO] Array#compact