我目前正在创建一个包装食谱,同时试图通过使用Rubocop和Foodcritic来限制自己的正确格式。不过,我不断收到以下错误:引号和厨师属性中冒号之间的区别是什么?
Use strings in preference to symbols to access node attributes
和
Access node attributes in a consistent manner
这使我想知道,是否有冒号和引号之间的区别吗?
我目前正在创建一个包装食谱,同时试图通过使用Rubocop和Foodcritic来限制自己的正确格式。不过,我不断收到以下错误:引号和厨师属性中冒号之间的区别是什么?
Use strings in preference to symbols to access node attributes
和
Access node attributes in a consistent manner
这使我想知道,是否有冒号和引号之间的区别吗?
是......冒号用于定义符号,而串由引号定义...所以:
是命名变量1
:'variable is 1'
一个符号,一个象征
'variable1'
- 是一个字符串
"variable#{1}"
- 是一个可以在其中定义变量的字符串。双引号解释字符串,而单引号使用字符串。
和
“variable1'.to_sym是一样的:变量1
正如Sid的答复中提到,:foo
是一个符号,而"foo"
和'foo'
都是字符串。对于节点属性,我们会自动为您转换事物,以便两种样式都可以工作Foodcritic规则是为了确保所有Chef代码中的一致风格。如果您没有理由不这样做,我们建议使用字符串样式,因为引用的字符串在很多编程语言中都很常见,所以更少用Ruby流利的读者来理解。