Skip to content

Instantly share code, notes, and snippets.

@carlosbaraza
Created July 17, 2018 14:11
Show Gist options
  • Save carlosbaraza/6a3547e94a0a14356549b595919b9012 to your computer and use it in GitHub Desktop.
Save carlosbaraza/6a3547e94a0a14356549b595919b9012 to your computer and use it in GitHub Desktop.
deep_symbolize_keys = lambda do |hash|
return hash unless hash.is_a?(Hash)
Hash[ hash.map do |key, value|
# if value is array, loop each element and recursively symbolize keys
if value.is_a? Array
value = value.map { |element| symbolize_keys.call(element) }
# if value is hash, recursively symbolize keys
elsif value.is_a? Hash
value = symbolize_keys.call(value)
end
[key.to_sym, value]
end ]
end
deep_symbolize_keys.call(params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment