我目前正在一个项目中使用sidekiq,我有以下yaml配置文件:
:concurrency: 5
:pidfile: /tmp/pids/sidekiq.pid
:logfile: log/sidekiq.log
staging:
:concurrency: 10
production:
:concurrency: 20
queues:
- default
我以前从未见过在键前面有一个冒号,但是省略这个冒号会产生有趣的结果。例如,在
:pidfile:
的情况下,如果前面有冒号,它将在没有冒号的地方创建/覆盖目标文件,它将使用已经存在的冒号,而不会对其进行写入。这是文档化的还是sidekiq对某些键的期望?
最佳答案
以冒号开头的yaml键在ruby中生成符号键,而没有冒号的键将生成字符串键:
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"
# ]
# }
注意:如果gem依赖于符号化键,则字符串化键不会覆盖其默认值。
关于ruby-on-rails - 前冒号:YAML语法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31673857/