Created
January 21, 2017 01:16
-
-
Save kenjij/e536cd5793e095f9d12c6a8f2397d56a to your computer and use it in GitHub Desktop.
Ruby: iterate through nested object and replace keys (convert to symbol)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convert all Hash keys to lowercase symbols | |
# @param obj [Object] any Ruby object | |
def keys_to_sym(obj) | |
case obj | |
when Array | |
obj.each do |v| | |
keys_to_sym(v) | |
end | |
when Hash | |
obj.keys.each do |k| | |
if k.class == String | |
obj[k.downcase.to_sym] = keys_to_sym(obj.delete(k)) | |
end | |
end | |
end | |
return obj | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment