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

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

instance method Hash#compact!

compact! -> self | nilRuby 2.4.0 から[permalink][rdoc][edit]

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

hash = {a: 1, b: nil, c: 3}
hash.compact!
p hash          # => {:a=>1, :c=>3}
p hash.compact! # =>  nil

[SEE_ALSO] Hash#compact, Array#compact!