class Hash unless method_defined?(:stringify_keys) def stringify_keys inject({}) do |options, (key, value)| options[key.to_s] = value options end end end unless method_defined?(:stringify_keys!) def stringify_keys! replace(stringify_keys) end end unless method_defined?(:symbolize_keys) def symbolize_keys inject({}) do |options, (key, value)| options[(key.to_sym rescue key) || key] = value options end end end unless method_defined?(:symbolize_keys!) def symbolize_keys! replace(symbolize_keys) end end unless method_defined?(:except) def except(*args) hash = dup args.each {|k| hash.delete(k) } hash end end unless method_defined?(:only) def only(*args) hash = {} args.each {|k| hash[k] = self[k] if self.has_key?(k) } hash end end def rename_keys(*args) keys = Hash[*args.flatten] keys.each { |k, v| self[v] = delete(k) if self[k] } self end def rename_keys!(*args) replace(rename_keys(*args)) end def symbolize_and_underscore_keys inject({}) do |options, (key, value)| options[(key.to_s.underscore.to_sym rescue key) || key] = value options end end def symbolize_and_underscore_keys! replace(symbolize_and_underscore_keys) end end