Skip to content

Instantly share code, notes, and snippets.

@bgroupe
Forked from pjb3/symbolize_keys.rb
Last active August 29, 2015 14:27
Show Gist options
  • Save bgroupe/dc01a7c68d4fe6847e6d to your computer and use it in GitHub Desktop.
Save bgroupe/dc01a7c68d4fe6847e6d to your computer and use it in GitHub Desktop.

Revisions

  1. @pjb3 pjb3 created this gist Jul 21, 2009.
    14 changes: 14 additions & 0 deletions symbolize_keys.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    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