2

我在厨师资源中有以下属性:

attribute :attribName, kind_of: String, name_attribute: true, default: 'big string sldkjslkdflksdksdlkff'

我想打破它,让它看起来不错,所以我这样做了:

attribute [
  :attribName,
  kind_of: String,
  name_attribute: true,
  default: 'big string sldkjslkdflksdksdlkff'
]

但是在收敛时出现错误:

   NoMethodError
   -------------
   undefined method `to_sym' for #<Array:0x00000004a63b60>
   Did you mean?  to_s
           to_yaml
           to_set

   Platform:
   ---------
   x64-mingw32    

   Running handlers:
   [2016-10-01T19:07:39-07:00] ERROR: Running exception handlers
   Running handlers complete
   [2016-10-01T19:07:39-07:00] ERROR: Exception handlers complete
   Chef Client failed. 0 resources updated in 11 seconds
   [2016-10-01T19:07:39-07:00] FATAL: Stacktrace dumped to C:/Users/ADMINI~1/AppData/Local/Temp/kitchen/cache/chef-stacktrace.out
   [2016-10-01T19:07:39-07:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
   [2016-10-01T19:07:39-07:00] FATAL: NoMethodError: undefined method `to_sym' for #<Array:0x00000004a63b60>
   Did you mean?  to_s
           to_yaml
           to_set

所以我认为attribute资源文件中的 s 只是接受一组参数并将 [..args..] 传递给它的方法。为什么这不起作用?我想我对在这种情况下什么类型的 ruby​​ 对象属性以及它们的行为感到困惑。

4

1 回答 1

2

attribute方法试图符号化第一个参数,即属性的名称。第二个参数看起来像是选项的散列,所以方法签名应该是这样的def attribute(name, options={}):当您将所有内容包裹在括号内时,您发送了一个数组作为第一个参数。

尝试像这样重新格式化它:

attribute :attribName,
  kind_of: String,
  name_attribute: true,
  default: 'big string sldkjslkdflksdksdlkff'

如果你真的不喜欢第一个参数,你可以使用 splat(我根本不喜欢这个):

attribute *[
  :attribName,
  {
    kind_of: String,
    name_attribute: true,
    default: 'big string sldkjslkdflksdksdlkff'
  }
]

这会将数组元素转换为参数。

于 2016-10-02T03:12:48.167 回答