Created
April 28, 2014 01:36
-
-
Save andrewpcone/11359798 to your computer and use it in GitHub Desktop.
Recursively symbolize_keys in a nested ruby hash
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
require 'active_support/core_ext/hash' | |
def symbolize_keys!(thing) | |
case thing | |
when Array | |
thing.each{|v| symbolize_keys!(v)} | |
when Hash | |
thing.symbolize_keys! | |
thing.values.each{|v| symbolize_keys!(v)} | |
end | |
thing | |
end | |
def symbolize_keys(thing) | |
case thing | |
when Array | |
thing.map{|v| symbolize_keys(v)} | |
when Hash | |
inj = thing.inject({}) {|h, (k,v)| h[k] = symbolize_keys(v); h} | |
inj.symbolize_keys | |
else | |
thing | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment