Rails load YAML to hash and reference by symbol

前端 未结 11 2048
慢半拍i
慢半拍i 2021-02-01 12:22

I am loading a YAML file in Rails 3.0.9 like this:

APP_CONFIG = YAML.load(File.read(File.expand_path(\'../app.yml\', __FILE__)))

It loads the a

11条回答
  •  鱼传尺愫
    2021-02-01 12:32

    1. Rails has a special method to symbolize keys.
    2. You can use load_file method and get rid of File.read
    3. Not sure if you need expand_path also, the default directory is rails root.

    I'd write it that simple:

    YAML::load_file('app.yml').symbolize_keys

提交回复
热议问题