Sha256: 84d688f85e8582ad607799d7a920debe388e59a81f0ffb4e9600f8189c82b111

Contents?: true

Size: 693 Bytes

Versions: 43

Compression:

Stored size: 693 Bytes

Contents

class Hash
  unless Hash.instance_methods(false).include?(:compact)
    # Returns a hash with non +nil+ values.
    #
    #   hash = { a: true, b: false, c: nil}
    #   hash.compact # => { a: true, b: false}
    #   hash # => { a: true, b: false, c: nil}
    #   { c: nil }.compact # => {}
    def compact
      self.select { |_, value| !value.nil? }
    end
  end

  unless Hash.instance_methods(false).include?(:compact!)
    # Replaces current hash with non +nil+ values.
    #
    #   hash = { a: true, b: false, c: nil}
    #   hash.compact! # => { a: true, b: false}
    #   hash # => { a: true, b: false}
    def compact!
      self.reject! { |_, value| value.nil? }
    end
  end
end

Version data entries

43 entries across 41 versions & 6 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/activesupport-5.0.7.1/lib/active_support/core_ext/hash/compact.rb
activesupport-4.2.11.3 lib/active_support/core_ext/hash/compact.rb
activesupport-4.2.11.2 lib/active_support/core_ext/hash/compact.rb
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/core_ext/hash/compact.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.7.2 lib/active_support/core_ext/hash/compact.rb
activesupport-4.2.11.1 lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.7.1 lib/active_support/core_ext/hash/compact.rb
activesupport-4.2.11 lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.7 lib/active_support/core_ext/hash/compact.rb
activesupport-4.2.10 lib/active_support/core_ext/hash/compact.rb
activesupport-4.2.10.rc1 lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.6 lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.6.rc1 lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.5 lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.5.rc2 lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.5.rc1 lib/active_support/core_ext/hash/compact.rb
tdiary-5.0.5 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/hash/compact.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/activesupport-5.0.2/lib/active_support/core_ext/hash/compact.rb
activesupport-4.2.9 lib/active_support/core_ext/hash/compact.rb