Sha256: 8b31a8fcbec7ed1c73ffe82f5b6d34895950b81e8d2a73d101d8132a0e11a095

Contents?: true

Size: 532 Bytes

Versions: 72

Compression:

Stored size: 532 Bytes

Contents

class Hash
  # 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

  # 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

Version data entries

72 entries across 69 versions & 13 rubygems

Version Path
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/compact.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/compact.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/compact.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/compact.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/compact.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/compact.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/compact.rb
abaci-0.3.0 vendor/bundle/gems/activesupport-5.0.1/lib/active_support/core_ext/hash/compact.rb
abaci-0.3.0 vendor/bundle/gems/activesupport-5.0.0/lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.1 lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.1.rc2 lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.1.rc1 lib/active_support/core_ext/hash/compact.rb
second_step-0.1.2 secondstep-notify-1.0.0-osx/lib/ruby/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0.1/lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.0.1 lib/active_support/core_ext/hash/compact.rb
activesupport-4.2.7.1 lib/active_support/core_ext/hash/compact.rb
activesupport-4.2.7 lib/active_support/core_ext/hash/compact.rb
activesupport-4.2.7.rc1 lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.0 lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.0.rc2 lib/active_support/core_ext/hash/compact.rb
activesupport-5.0.0.racecar1 lib/active_support/core_ext/hash/compact.rb