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

instance method Hash#compact

compact -> HashRuby 2.4.0 から[permalink][rdoc][edit]
() → ::Hash[K, V]

自身から value が nil のもの取り除いた Hash を生成して返します。

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

[SEE_ALSO] Hash#compact!, Array#compact