Created
December 14, 2012 15:47
-
-
Save bogdanRada/4286379 to your computer and use it in GitHub Desktop.
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(obj) | |
case obj | |
when Array | |
obj.inject([]){|res, val| | |
res << case val | |
when Hash, Array | |
symbolize_keys(val) | |
else | |
val | |
end | |
res | |
} | |
when Hash | |
obj.inject({}){|res, (key, val)| | |
nkey = case key | |
when String | |
key.to_sym | |
else | |
key | |
end | |
nval = case val | |
when Hash, Array | |
symbolize_keys(val) | |
else | |
val | |
end | |
res[nkey] = nval | |
res | |
} | |
else | |
obj | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@softbrada Thank you for this. I use your code as core extension of the Hash class. This way I avoid to use larger Rails gems.