I have a non-Rails project in which I am loading some settings from a YAML file:
config = YAML::load(File.open("#{LOG_ROOT}/config/database.yml"))
I can only access this hash like config["host"]
, config["username"]
etc.
I want indifferent access so I can use both :host
and "host"
.
The reason is, one of the gems in the project to which I am passing this hash seems to be accessing it using symbols and it fails currently.
What is best way to create a hash with indifferent access in this scenario?
You lose nothing except a few kB of disk space by installing the Active Support gem. In your code, you require only the function you want:
require 'active_support/core_ext/hash/indifferent_access'
That way, you can be sure you are not getting anything else to mess up your namespace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With