Rails load YAML to hash and reference by symbol

前端 未结 11 2077
慢半拍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:43

    Psych (a.k.a. YAML) provides the keyword argument :symbolize_names to load keys as symbols. See method reference

    file_path = File.expand_path('../app.yml', __FILE__)
    yaml_contents = File.read(file_path)
    
    APP_CONFIG = YAML.safe_load(yaml_contents, symbolize_names: true)
    

提交回复
热议问题