From: Gregory Seidman Date: 2008-01-02T08:58:37+09:00 Subject: Re: transform hash key from string into symbol On Wed, Jan 02, 2008 at 08:26:31AM +0900, Old Echo wrote: > Hello Rubyists, > When dealing with hashes, I like using symbols for key names instead of > strings. Is there an easy way to convert hash keys from symbols into > strings? > > E.g. I'd like to do something like this: > > my_hash = YAML.load_file('my_file.yml') > => { 'one' => 1, 'two' => 2, 'three' => 3 } > my_hash.super_cool_hash_key_string_to_sym_transformation #one line of > code? > ... > my_hash > => { :one => 1, :two => 2, :three => 3 } > > Please display your ninja skills. : ) class Hash def symbolize_keys replace(inject({}) { |h,(k,v)| h[k.to_sym] = v; h }) end end > Thanks, > kodama --Greg