3

I set up an environment YAML file for environment specific variables like username and password. To use these variables in my app, I need to use APP_CONFIG['username'] instead of APP_CONFIG[:username]. Why is this? How would I enable the latter instead? Not a major issue, but it bothers me to not know the cause of the difference.

config/initializers/load_app_config.rb

APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")[Rails.env]

config/app_config.yml

development:
  username: development_name
  password: secret

production:
  username: production_name
  password: super_secret

2 Answers 2

9

By default, a YAML key is rendered as String.

development:
  username: development_name
  password: secret

is accessible by

APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")[Rails.env]
APP_CONFIG['development']['username']
# => "development_name"

I you want a specific key to be a symbol, you should prefix it with : in the YAML file.

development:
  :username: development_name
  :password: secret

APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")[Rails.env]
APP_CONFIG['development'][:username]
# => "development_name"
APP_CONFIG['development']['username']
# => nil

Normally, this is not done because this is a specific Ruby behavior. Other languages might not be happy about the leading :.

If you specifically wants to access keys as symbol, you can either use symbolize_keys!

APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")[Rails.env].simbolize_keys!

but most of the time, the effort is not worth. Internally, the 90% of libraries converts Symbols to Strings during a comparison, especially when you deal with hashes with indifferent access. So, at the end of the story, you might want to keep strings in this case.

Last option would be to create a HashWithIndifferentAccess

APP_CONFIG = HashWithIndifferentAccess.new(YAML.load_file("#{Rails.root}/config/app_config.yml")[Rails.env])

This will allow you to access

APP_CONFIG[:development][:username]
APP_CONFIG['development'][:username]
APP_CONFIG['development']['username']

indifferently. It works by storing the hash keys to string internally and converting the request to [] to string, so that it always works. This is the class used by several Rails components, including the famous params[] hash in the controllers.

1
  • Thanks very much @Simone, for the very detailed answer. It's very helpful to know the rationale behind the answer!
    – umezo
    Oct 3, 2012 at 3:54
2

Use symbolize_keys on the hash returned by YAML.load_file

3
  • Perfect, thanks @rmk. Is there any important reason to choose one way over another? Or is it just personal preference (ie. "keys look cooler")
    – umezo
    Oct 2, 2012 at 23:15
  • 1
    Well, symbols are faster to access than strings, but I do not believe it makes a big difference. It's just that it's more rubyish to use symbols rather than keys.
    – rmk
    Oct 3, 2012 at 17:32
  • Thanks @rmk. That was what I was thinking as well. Simone had an interesting point though: "Internally, the 90% of libraries converts Symbols to Strings during a comparison, especially when you deal with hashes with indifferent access. So, at the end of the story, you might want to keep strings in this case."
    – umezo
    Oct 3, 2012 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.