Colon in the front: YAML syntax

前端 未结 2 1177
情书的邮戳
情书的邮戳 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条回答
  •  不知归路
    2021-02-07 10:49

    YAML keys starting with a colon generate symbolized keys in Ruby, whereas keys without a colon will generate stringified keys:

    require 'yaml'
    
    string =<<-END_OF_YAML
    :concurrency: 5
    :pidfile: /tmp/pids/sidekiq.pid
    :logfile: log/sidekiq.log
    staging:
      :concurrency: 10
    production:
      :concurrency: 20
    queues:
      - default
    END_OF_YAML
    
    YAML.load(string)
    # {
    #     :concurrency => 5,
    #     :pidfile     => "/tmp/pids/sidekiq.pid",
    #     :logfile     => "log/sidekiq.log",
    #     "staging"    => {
    #         :concurrency => 10
    #     },
    #     "production" => {
    #         :concurrency => 20
    #     },
    #     "queues"     => [
    #         [0] "default"
    #     ]
    # }
    

    Note: If a gem depends on symbolized keys then the stringified keys will not override its defaults.

提交回复
热议问题