前面的冒号:YAML 语法 [英] Colon in the front: YAML syntax
问题描述
我目前在一个项目中使用 Sidekiq,我有以下 YAML 配置文件:
:concurrency: 5:pidfile:/tmp/pids/sidekiq.pid:logfile: 日志/sidekiq.log分期::并发:10生产::并发:20队列:- 默认
我以前从未见过在键前面有一个冒号,但省略该冒号会产生有趣的结果.例如,在 :pidfile:
的情况下,如果前面有冒号,它会创建/覆盖没有它的目标文件,它使用已经存在的文件并且不写入它.>
这是在某处记录的还是仅仅是 Sidekiq 期望某些密钥的方式?
以冒号开头的 YAML 键在 Ruby 中生成符号化键,而没有冒号的键将生成字符串化键:
需要'yaml'字符串 =<<-END_OF_YAML:并发:5:pidfile:/tmp/pids/sidekiq.pid:logfile: 日志/sidekiq.log分期::并发:10生产::并发:20队列:- 默认END_OF_YAMLYAML.load(字符串)# {# :并发=>5、# :pidfile =>"/tmp/pids/sidekiq.pid",# :logfile =>"日志/sidekiq.log",#分期"=>{# :并发=>10# },# "生产" =>{# :并发=>20# },#队列"=>[# [0] "默认"#]# }
注意:如果 gem 依赖于符号化的键,那么字符串化的键将不会覆盖其默认值.
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
staging:
:concurrency: 10
production:
:concurrency: 20
queues:
- default
I haven't seen having a colon in front of a key before but omitting that colon produces interesting results. In the case of the :pidfile:
for example, with the colon in front it creates/overrides the destination file where is without it, it uses the one already there and does not write to it.
Is this documented somewhere or is this simply how Sidekiq expects certain keys?
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.
这篇关于前面的冒号:YAML 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!