Created
March 9, 2015 21:56
-
-
Save michaelachrisco/a3aa3ed92d5e34f62b72 to your computer and use it in GitHub Desktop.
Symbolize Array of Hashes in Ruby
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
# Deep symbolizes keys in hash | |
class Hash | |
def deep_sybolize_keys(value = self) | |
case value | |
when Array | |
value.map { |v| deep_sybolize_keys(v) } | |
when Hash | |
symbolize_hash(value) | |
else | |
value | |
end | |
end | |
def symbolize_hash(value) | |
Hash[value.map { |k, v| [k.to_sym, deep_sybolize_keys(v)] }] | |
end | |
end |
Author
michaelachrisco
commented
Mar 9, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment