Sha256: 68acadef30c1b159f739411a45e5dd5d65bfd44721d0f1b726631aee6ac91110

Contents?: true

Size: 1.55 KB

Versions: 7

Compression:

Stored size: 1.55 KB

Contents

class Hash
  # Slice a hash to include only the given keys. This is useful for
  # limiting an options hash to valid keys before passing to a method:
  #
  #   def search(criteria = {})
  #     assert_valid_keys(:mass, :velocity, :time)
  #   end
  #
  #   search(options.slice(:mass, :velocity, :time))
  #
  # If you have an array of keys you want to limit to, you should splat them:
  #
  #   valid_keys = [:mass, :velocity, :time]
  #   search(options.slice(*valid_keys))
  def slice(*keys)
    keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
    hash = self.class.new
    keys.each { |k| hash[k] = self[k] if has_key?(k) }
    hash
  end unless method_defined?(:slice)

  # Replaces the hash with only the given keys.
  # Returns a hash containing the removed key/value pairs
  # @example
  #   hsh = {:a => 1, :b => 2, :c => 3, :d => 4}
  #   hsh.slice!(:a, :b)
  #   # => {:c => 3, :d =>4}
  #   hsh
  #   # => {:a => 1, :b => 2}
  def slice!(*keys)
    keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
    omit = slice(*self.keys - keys)
    hash = slice(*keys)
    replace(hash)
    omit
  end unless method_defined?(:slice!)

  # Removes the given keys from the hash
  # Returns a hash containing the removed key/value pairs
  #
  # @example
  #   hsh = {:a => 1, :b => 2, :c => 3, :d => 4}
  #   hsh.extract!(:a, :b)
  #   # => {:a => 1, :b => 2}
  #   hsh
  #   # => {:c => 3, :d =>4}
  def extract!(*keys)
    result = {}
    keys.each {|key| result[key] = delete(key) }
    result
  end unless method_defined?(:extract!)
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gorillib-0.0.8 lib/gorillib/hash/slice.rb
gorillib-0.0.7 lib/gorillib/hash/slice.rb
gorillib-0.0.6 lib/gorillib/hash/slice.rb
gorillib-0.0.5 lib/gorillib/hash/slice.rb
gorillib-0.0.4 lib/gorillib/hash/slice.rb
gorillib-0.0.3 lib/gorillib/hash/slice.rb
gorillib-0.0.2 lib/gorillib/hash/slice.rb