instance method compact!

Ruby on Rails 5.2.8.1

Since v4.1.16 Last seen in v5.2.8.1

Available in: v4.1.16 v4.2.9 v5.2.8.1

Signature

compact!()

Replaces current hash with non nil values. Returns nil if no changes were made, otherwise returns the hash.

hash = { a: true, b: false, c: nil }
hash.compact!        # => { a: true, b: false }
hash                 # => { a: true, b: false }
{ c: true }.compact! # => nil
Source
# File activesupport/lib/active_support/core_ext/hash/compact.rb, line 25
    def compact!
      reject! { |_, value| value.nil? }
    end

Defined in activesupport/lib/active_support/core_ext/hash/compact.rb line 25 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Hash

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close