Skip to content

Instantly share code, notes, and snippets.

@andrewpcone
Created April 28, 2014 01:36
Show Gist options
  • Save andrewpcone/11359798 to your computer and use it in GitHub Desktop.
Save andrewpcone/11359798 to your computer and use it in GitHub Desktop.
Recursively symbolize_keys in a nested ruby hash
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