Sha256: 20b3a3cddc327ee3738587a13991739d84601dac4c323ebcba8b12a05e6c7ff5

Contents?: true

Size: 618 Bytes

Versions: 24

Compression:

Stored size: 618 Bytes

Contents

# frozen_string_literal: true
module HashExtensions
  refine Hash do
    # 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
      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!
      reject! {|_, value| value.nil? }
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
finapps-2.1.4 lib/core_extensions/hash/compact.rb
finapps-2.1.3 lib/core_extensions/hash/compact.rb
finapps-2.1.2 lib/core_extensions/hash/compact.rb
finapps-2.1.1 lib/core_extensions/hash/compact.rb
finapps-2.0.30 lib/core_extensions/hash/compact.rb
finapps-2.0.29 lib/core_extensions/hash/compact.rb
finapps-2.0.28 lib/core_extensions/hash/compact.rb
finapps-2.0.27 lib/core_extensions/hash/compact.rb
finapps-2.0.26 lib/core_extensions/hash/compact.rb
finapps-2.0.25 lib/core_extensions/hash/compact.rb
finapps-2.0.24 lib/core_extensions/hash/compact.rb
finapps-2.0.23 lib/core_extensions/hash/compact.rb
finapps-2.0.22 lib/core_extensions/hash/compact.rb
finapps-2.0.21 lib/core_extensions/hash/compact.rb
finapps-2.0.20 lib/core_extensions/hash/compact.rb
finapps-2.0.19 lib/core_extensions/hash/compact.rb
finapps-2.0.17 lib/core_extensions/hash/compact.rb
finapps-2.0.16 lib/core_extensions/hash/compact.rb
finapps-2.0.15 lib/core_extensions/hash/compact.rb
finapps-2.0.14 lib/core_extensions/hash/compact.rb