From: James Britt Date: 2004-11-03T06:55:38+09:00 Subject: Re: Converting hash keys to symbols Jamis Buck wrote: > Golf question... > > Is there an elegant way of converting all of the keys of a hash to a > symbol? The solution can assume that the existing keys are either > strings or symbols. Also, if a key is a string, it may contain a dash > character which must be converted to an underscore. > > Something like this, only more elegant, would be nice: > > a = some_hash_of_strings_and_symbols > new_a = Hash[*a.collect { |k,v| > k = k.gsub(/-/,"_").intern if k.is_a?(String) > [k,v] }.flatten] > > Any takers? > > - Jamis > I'd be inclined to add 'intern' to Symbol, and just call intern on everything in the key set. If you get a Symbol, then Symbol#intern just returns self. And override intern in String to do character subs. James