Created
January 23, 2014 20:01
-
-
Save neektza/8585746 to your computer and use it in GitHub Desktop.
Updated Avdi's symbolize_keys
This file contains 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
def symbolize_keys(x) | |
if x.is_a? Hash | |
x.inject({}) do |result, (key, value)| | |
new_key = case key | |
when String then key.to_sym | |
else key | |
end | |
new_value = case value | |
when Hash then symbolize_keys(value) | |
when Array then symbolize_keys(value) | |
else value | |
end | |
result[new_key] = new_value | |
result | |
end | |
elsif x.is_a? Array | |
x.map {|el| symbolize_keys(el)} | |
else | |
x | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment