Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/fat_core/hash.rb
Instance Method Summary collapse
-
#delete_with_value(v) ⇒ Object
Remove from the hash all keys that have values == to given value or that include the given value if the hash has an Enumerable for a value.
-
#keys_with_value(val) ⇒ Object
Return all keys in hash that have a value == to the given value or have an Enumerable value that includes the given value.
- #remap_keys(key_map = {}) ⇒ Object
Instance Method Details
permalink #delete_with_value(v) ⇒ Object
Remove from the hash all keys that have values == to given value or that include the given value if the hash has an Enumerable for a value
16 17 18 19 20 21 |
# File 'lib/fat_core/hash.rb', line 16 def delete_with_value(v) keys_with_value(v).each do |k| delete(k) end self end |
permalink #keys_with_value(val) ⇒ Object
Return all keys in hash that have a value == to the given value or have an Enumerable value that includes the given value.
4 5 6 7 8 9 10 11 12 |
# File 'lib/fat_core/hash.rb', line 4 def keys_with_value(val) result = [] each_pair do |k, v| if self[k] == val || (v.respond_to?(:include?) && v.include?(val)) result << k end end result end |
permalink #remap_keys(key_map = {}) ⇒ Object
[View source]
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fat_core/hash.rb', line 23 def remap_keys(key_map = {}) new_hash = {} each_pair do |key, val| if key_map.has_key?(key) new_hash[key_map[key]] = val else new_hash[key] = val end end new_hash end |