Sha256: 111abf27a9d01e9b7ec9bfa571da91e0180148e48c1626e703bb259489be0c8d

Contents?: true

Size: 616 Bytes

Versions: 13

Compression:

Stored size: 616 Bytes

Contents

require "facets/hash/except"

class Hash

  unless defined?(::ActiveSupport)  # ActiveSupport

    # Returns a new hash with only the given keys.

    def slice(*keep_keys)
      h = {}
      keep_keys.each do |key|
        h[key] = fetch(key)
      end
      h
    end

    # Replaces hash with a new hash having only the given keys.
    # This return the hash of keys removed.
    #
    #   h = {:a=>1, :b=>2}
    #   h.slice!(:a)  #=> {:a=>1}
    #   h             #=> {:b=>2}
    #
    def slice!(*keep_keys)
      removed = except(*keep_keys)
      replace(slice(*keep_keys))
      removed
    end

  end

end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
facets-2.8.4 lib/core/facets/hash/slice.rb
facets-2.8.3 lib/core/facets/hash/slice.rb
facets-2.8.2 lib/core/facets/hash/slice.rb
facets-2.8.1 lib/core/facets/hash/slice.rb
facets-2.8.0 lib/core/facets/hash/slice.rb
facets-2.7.0 lib/core/facets/hash/slice.rb
facets-2.6.0 lib/core/facets/hash/slice.rb
facets-2.4.4 lib/core/facets/hash/slice.rb
facets-2.4.5 lib/core/facets/hash/slice.rb
facets-2.5.1 lib/core/facets/hash/slice.rb
facets-2.5.0 lib/core/facets/hash/slice.rb
facets-2.5.2 lib/core/facets/hash/slice.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/hash/slice.rb