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.0.13 lib/core_extensions/hash/compact.rb
finapps-2.0.12 lib/core_extensions/hash/compact.rb
finapps-2.0.11 lib/core_extensions/hash/compact.rb
finapps-2.0.10 lib/core_extensions/hash/compact.rb