Class: Hash

Inherits:
Object show all
Defined in:
lib/awsbase/support.rb

Overview

:nodoc:

Direct Known Subclasses

Aws::AwsResponseObjectHash

Instance Method Summary collapse

Instance Method Details

#slice(*keys) ⇒ Object

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))


123
124
125
126
127
128
# File 'lib/awsbase/support.rb', line 123

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

#symbolize_keysObject

Return a new hash with all keys converted to symbols.



103
104
105
106
107
108
# File 'lib/awsbase/support.rb', line 103

def symbolize_keys
  inject({}) do |options, (key, value)|
    options[key.to_sym] = value
    options
  end
end