Rails load YAML to hash and reference by symbol

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

    Just use appropriate option in your YAML parser. For instance, symbolize_names in Psych:

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

    See RDoc: https://ruby-doc.org/stdlib-2.6.1/libdoc/psych/rdoc/Psych.html#method-c-load.

提交回复
热议问题