J
Jamis Buck
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
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