Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash with indifferent access

Tags:

ruby

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?

like image 983
Josnidhin Avatar asked May 28 '12 07:05

Josnidhin


1 Answers

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.

like image 171
Boris Stitnicky Avatar answered Nov 01 '22 18:11

Boris Stitnicky