Sha256: 57b6864c57f4f7aa00d314c5ecb2c8a158378d5d7e23bbc984c9429429b08c2e

Contents?: true

Size: 632 Bytes

Versions: 4

Compression:

Stored size: 632 Bytes

Contents

class Hash

  # Returns a new hash with only the given keys.
  #
  #   h = {:a=>1, :b=>2}
  #   h.slice(:a)  #=> {:a=>1}
  #
  def slice(*keep_keys)
    hash = {}
    keep_keys.each do |key|
      hash[key] = fetch(key)
    end
    hash
  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)  #=> {:b=>2}
  #   h             #=> {:a=>1}
  #
  # Returns a Hash of the removed pairs.
  def slice!(*keep_keys)
    rejected = keys - keep_keys
    removed = {}
    rejected.each{ |k| removed[k] = delete(k) }
    removed
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facets-2.9.1 lib/core/facets/hash/slice.rb
facets-2.9.0 lib/core/facets/hash/slice.rb
facets-2.9.0.pre.2 lib/core/facets/hash/slice.rb
facets-2.9.0.pre.1 lib/core/facets/hash/slice.rb