2016-10-02 115 views
2

我有一個廚師資源的以下屬性:廚師LWRP的屬性方法是什麼?我可以通過哪些方法?

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 IN的資源文件只是方法是接受參數數組並通過[..args ..]它是一回事。爲什麼這不起作用?我想我對這種情況下的ruby對象屬性類型以及它們的行爲是什麼感到困惑。

回答

2

attribute方法試圖象徵第一個參數,它是屬性的名稱。第二個參數看起來像是選項的散列,所以方法簽名應該看起來像這樣:def attribute(name, options={})。當您將所有內容都包含在括號中時,您發送了一個數組作爲第一個參數。

嘗試重新格式化是這樣的:

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

如果你真的不認爲第一個參數在同一樣,你可以用圖示(我不喜歡這個在所有):

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

這會將數組元素轉換爲參數。

+1

除非你是一個真正的老版本的廚師,請使用'property'而不是'attribute'。後者僅適用於向後兼容。 – coderanger

相關問題