Module: Utilities::Hashes

Defined in:
lib/utilities/hashes.rb

Class Method Summary (collapse)

Class Method Details

+ (Hash) delete_keys(hsh, array)

Delete all matching keys in array from the hsh

Parameters:

  • hsh (Hash)
  • array (Array)

Returns:

  • (Hash)


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

+ (Object) puts_collisions(a, b)

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)

Parameters:

  • a (Hash)
  • b (Hash)


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

+ (Hash) symbolize_keys(hash)

Parameters:

  • hash (Hash)

Returns:

  • (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