Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/fat_core/hash.rb

Instance Method Summary collapse

Instance Method Details

#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
# File 'lib/fat_core/hash.rb', line 16

def delete_with_value(v)
  keys_with_value(v).each do |k|
    delete(k)
  end
end

#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

#remap_keys(key_map = {}) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fat_core/hash.rb', line 22

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