Class: Hash
Overview
:nodoc:
Direct Known Subclasses
Instance Method Summary collapse
-
#slice(*keys) ⇒ Object
Slice a hash to include only the given keys.
-
#symbolize_keys ⇒ Object
Return a new hash with all keys converted to symbols.
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(.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(.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_keys ⇒ Object
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 |, (key, value)| [key.to_sym] = value end end |