Module: Utilities::Hashes
- Defined in:
- lib/utilities/hashes.rb
Class Method Summary collapse
-
.delete_keys(hsh, array) ⇒ Hash
Delete all matching keys in array from the hsh.
-
.delete_nil_and_empty_hash_values(hsh) ⇒ Hash
Delete all nil and empty hashes values.
-
.puts_collisions(a, b) ⇒ Object
Pass two hashes, a and b, print out those situations in which a.merge(b) would change a value assigned in a to something different (assigned in b).
-
.set_unless_nil(hsh, key, value) ⇒ Object
Sets value at key in hsh iff value is not nil.
- .symbolize_keys(hash) ⇒ Hash
Class Method Details
.delete_keys(hsh, array) ⇒ Hash
Delete all matching keys in array from the hsh
20 21 22 |
# File 'lib/utilities/hashes.rb', line 20 def self.delete_keys(hsh, array) hsh.delete_if{|k,v| array.include?(k)} end |
.delete_nil_and_empty_hash_values(hsh) ⇒ Hash
Delete all nil and empty hashes values
44 45 46 47 48 49 |
# File 'lib/utilities/hashes.rb', line 44 def self.delete_nil_and_empty_hash_values(hsh) hsh.each do |key, value| hsh.delete(key) if hsh[key].nil? || hsh[key] == {} end hsh end |
.puts_collisions(a, b) ⇒ Object
Pass two hashes, a and b, print out those situations in which a.merge(b) would change a value assigned in a to something different (assigned in b)
8 9 10 11 12 13 14 |
# File 'lib/utilities/hashes.rb', line 8 def self.puts_collisions(a, b) a.each do |i,j| if b[i] && !j.blank? && b[i] != j puts "#{i}: [#{j}] != [#{b[i]}]" end end end |
.set_unless_nil(hsh, key, value) ⇒ Object
Sets value at key in hsh iff value is not nil
37 38 39 |
# File 'lib/utilities/hashes.rb', line 37 def self.set_unless_nil(hsh, key, value) hsh[key] = value unless value.nil? end |
.symbolize_keys(hash) ⇒ Hash
26 27 28 29 30 31 |
# File 'lib/utilities/hashes.rb', line 26 def self.symbolize_keys(hash) hash.inject({}) do |h, (k, v)| h[k.is_a?(String) ? k.to_sym : k] = (v.is_a?(Hash) ? symbolize_keys(v) : v) h end end |