Best way to convert strings to symbols in hash
Here's a better method, if you're using Rails:
params.symbolize_keys
The end.
If you're not, just rip off their code (it's also in the link):
myhash.keys.each do |key| myhash[(key.to_sym rescue key) || key] = myhash.delete(key)end
In Ruby >= 2.5 (docs) you can use:
my_hash.transform_keys(&:to_sym)
Using older Ruby version? Here is a one-liner that will copy the hash into a new one with the keys symbolized:
my_hash = my_hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
With Rails you can use:
my_hash.symbolize_keysmy_hash.deep_symbolize_keys
For the specific case of YAML in Ruby, if the keys begin with ':
', they will be automatically interned as symbols.
require 'yaml'require 'pp'yaml_str = "connections: - host: host1.example.com port: 10000 - host: host2.example.com port: 20000"yaml_sym = ":connections: - :host: host1.example.com :port: 10000 - :host: host2.example.com :port: 20000"pp yaml_str = YAML.load(yaml_str)puts yaml_str.keys.first.classpp yaml_sym = YAML.load(yaml_sym)puts yaml_sym.keys.first.class
Output:
# /opt/ruby-1.8.6-p287/bin/ruby ~/test.rb{"connections"=> [{"port"=>10000, "host"=>"host1.example.com"}, {"port"=>20000, "host"=>"host2.example.com"}]}String{:connections=> [{:port=>10000, :host=>"host1.example.com"}, {:port=>20000, :host=>"host2.example.com"}]}Symbol