Class: Hash
Overview
Copyright (c) 2010-2015 GoodData Corporation. All rights reserved. This source code is licensed under the BSD-style license found in the LICENSE file in the root directory of this source tree.
Direct Known Subclasses
Instance Method Summary collapse
- #compact ⇒ Object
-
#except(*keys) ⇒ Object
Return a hash that includes everything but the given keys.
-
#except!(*keys) ⇒ Object
Replaces the hash without the given keys.
- #slice(*keys) ⇒ Object
Instance Method Details
#compact ⇒ Object
35 36 37 |
# File 'lib/gooddata/extensions/hash.rb', line 35 def compact select { |_, value| !value.nil? } end |
#except(*keys) ⇒ Object
Return a hash that includes everything but the given keys. This is useful for limiting a set of parameters to everything but a few known toggles:
@person.update_attributes(params[:person].except(:admin))
If the receiver responds to +convert_key+, the method is called on each of the arguments. This allows +except+ to play nice with hashes with indifferent access for instance:
=> 1.with_indifferent_access.except(:a) # => {} => 1.with_indifferent_access.except("a") # => {}
20 21 22 |
# File 'lib/gooddata/extensions/hash.rb', line 20 def except(*keys) dup.except!(*keys) end |
#except!(*keys) ⇒ Object
Replaces the hash without the given keys.
25 26 27 28 |
# File 'lib/gooddata/extensions/hash.rb', line 25 def except!(*keys) keys.each { |key| delete(key) } self end |
#slice(*keys) ⇒ Object
30 31 32 33 |
# File 'lib/gooddata/extensions/hash.rb', line 30 def slice(*keys) keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if key?(k) } end |