Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Hash#compact!

() -> self?
compact! → self or nil

If self contains any nil-valued entries, returns self with all nil-valued entries removed; returns nil otherwise:

Example
h = {foo: 0, bar: nil, baz: 2, bat: nil}
h.compact!
h          # => {foo: 0, baz: 2}
h.compact! # => nil

Related: see Methods for Deleting.