Skip to content

Instantly share code, notes, and snippets.

@pedrocarrico
Forked from pjb3/symbolize_keys.rb
Created August 30, 2012 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrocarrico/3525845 to your computer and use it in GitHub Desktop.
Save pedrocarrico/3525845 to your computer and use it in GitHub Desktop.
module HashExtensions
def symbolize_keys
inject({}) do |acc, (k,v)|
key = String === k ? k.to_sym : k
value = Hash === v ? v.symbolize_keys : v
acc[key] = value
acc
end
end
end
Hash.send(:include, HashExtensions)
hash = {"drinks"=>{"gibson"=>{"garnish"=>"onion"}, "martini"=>{"garnish"=>"olive"}}}
puts hash.symbolize_keys.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment