Class: Hash

Inherits:
Object show all
Defined in:
lib/eluvia/utils/hash_and_array.rb

Instance Method Summary collapse

Instance Method Details

#camel_keysObject

Convert all keys in hash into camel case



85
86
87
88
89
# File 'lib/eluvia/utils/hash_and_array.rb', line 85

def camel_keys
  self.each_with_object({}) { |(k, v), memo|
    memo[k.to_s.camelize] = v
  }
end

#deep_merge!(other_hash) ⇒ Object

Deep merging of hashes By Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809



93
94
95
96
# File 'lib/eluvia/utils/hash_and_array.rb', line 93

def deep_merge!(other_hash)
  merger = proc { |_k, v1, v2| v1.is_a?(::Hash) && v2.is_a?(::Hash) ? v1.merge(v2, &merger) : v2 }
  self.merge!(other_hash, &merger)
end

#integerize_keysObject

Convert all keys in hash into integers



71
72
73
74
75
# File 'lib/eluvia/utils/hash_and_array.rb', line 71

def integerize_keys
  self.each_with_object({}) { |(k, v), memo|
    memo[k.to_i] = v
  }
end

#snake_keysObject

Convert all keys in hash into snake case



78
79
80
81
82
# File 'lib/eluvia/utils/hash_and_array.rb', line 78

def snake_keys
  self.each_with_object({}) { |(k, v), memo|
    memo[k.to_s.underscore] = v
  }
end