Module: FatCore::Hash
- Included in:
- Hash
- Defined in:
- lib/fat_core/hash.rb
Overview
It provides a couple of methods for manipulating the keys of a Hash:
#remap_keys for translating the current set of keys to a new set provided by
a Hash of old to new keys, and #replace_keys for doing a similar operation
with an Array of new keys. Along the same line, the method #keys_with_value
will return the keys in a Hash equal to the given value of any of an Array of
values.
It also provides a method for deleting all entries in a Hash whose value match
a single value or any one of an Array of values in #delete_with_value
Finally, it provides an #each_pair-like method, #each_pair_with_flags,
that yields each key-value pair of the Hash along with two boolean flags that
indicate whether the element is the first or last in the Hash.
Enumerable Extensions collapse
-
#each_pair_with_flags ⇒ Hash
Yield each key-value pair in the Hash together with two boolean flags that indicate whether the item is the first or last item in the Hash.
Deletion collapse
-
#delete_with_value(*val) ⇒ Hash
Return a copy of the Hash a Hash with all keys that have values == to the given value or values.
-
#delete_with_value!(*val) ⇒ Hash
Remove from the hash in place all keys that have values == to the given value or values.
Key Manipulation collapse
- #<<(other) ⇒ Object
-
#keys_with_value(*vals) ⇒ Array<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 = {}) ⇒ Hash
Change each key of this Hash to its value in
key_map. -
#replace_keys(new_keys) ⇒ Hash
Change the keys of this Hash to new_keys, an array of keys of the same size as the Array self.keys.
Instance Method Details
#<<(other) ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/fat_core/hash.rb', line 170 def <<(other) case other when Hash merge(other) when Enumerable merge(other.flatten.each_slice(2).to_h) end end |
#delete_with_value(*val) ⇒ Hash
Return a copy of the Hash a Hash with all keys that have values == to the given value or values.
99 100 101 102 103 104 105 106 107 |
# File 'lib/fat_core/hash.rb', line 99 def delete_with_value(*val) hsh = clone val.each do |v| hsh.keys_with_value(v).each do |k| hsh.delete(k) end end hsh end |
#delete_with_value!(*val) ⇒ Hash
Remove from the hash in place all keys that have values == to the given value or values.
77 78 79 80 81 82 83 84 |
# File 'lib/fat_core/hash.rb', line 77 def delete_with_value!(*val) val.each do |v| keys_with_value(v).each do |k| delete(k) end end self end |
#each_pair_with_flags ⇒ Hash
Yield each key-value pair in the Hash together with two boolean flags that indicate whether the item is the first or last item in the Hash.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/fat_core/hash.rb', line 51 def each_pair_with_flags last_k = size - 1 k = 0 each_pair do |key, val| first = k.zero? last = (k == last_k) yield(key, val, first, last) k += 1 end self end |
#keys_with_value(*vals) ⇒ Array<Object>
Return all keys in hash that have a value == to the given value or have an Enumerable value that includes the given value.
121 122 123 124 125 126 127 128 129 |
# File 'lib/fat_core/hash.rb', line 121 def keys_with_value(*vals) keys = [] vals.each do |val| each_pair do |k, v| keys << k if self[k] == val || (v.respond_to?(:include?) && v.include?(val)) end end keys end |
#remap_keys(key_map = {}) ⇒ Hash
Change each key of this Hash to its value in key_map. Keys not appearing in
the key_map remain in the result Hash.
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/fat_core/hash.rb', line 141 def remap_keys(key_map = {}) new_hash = {} each_pair do |key, val| if key_map.key?(key) new_hash[key_map[key]] = val else new_hash[key] = val end end new_hash end |
#replace_keys(new_keys) ⇒ Hash
Change the keys of this Hash to new_keys, an array of keys of the same size as the Array self.keys.
164 165 166 167 168 |
# File 'lib/fat_core/hash.rb', line 164 def replace_keys(new_keys) raise ArgumentError, 'replace_keys: new keys size differs from key size' unless keys.size == new_keys.size to_a.each_with_index.to_h { |(_k, v), i| [new_keys[i], v] } end |