Colon in the front: YAML syntax

前端 未结 2 1221
情书的邮戳
情书的邮戳 2021-02-07 09:56

I\'m currently using Sidekiq in a project and I have the following YAML config file:

:concurrency: 5
:pidfile: /tmp/pids/sidekiq.pid
:logfile: log/sidekiq.log
st         


        
2条回答
  •  萌比男神i
    2021-02-07 10:45

    It is actually not sidekiq specific. The colon in front of a key just makes this key a symbol instead of a string:

    # example.yml
    a:
      value: 1
    :b:
      value: 2
    
    
    yaml = YAML.load_file('example.yml')
    
    yaml["a"] => { "value" => 1 }
    yaml[:b] => { "value" => 1 }
    

    So if your code accesses the config with key symbols, you should either add a colon in front of the key in the yaml file, or use some conversion of keys like #with_indifferent_access for the result hash (after parsing the yaml file)

提交回复
热议问题