我正在为一本厨师食谱写一篇LWRP,而我遇到了一个奇怪的问题,即这个属性在一行上看起来完全有效,而nil
则在下一行。source
。
来自提供商代码,def create_check
cookbook_file get_check_filename(@current_resource.checkname) do
source "checks/#{@current_resource.checkname}" # undefined method `checkname' for nil:NilClass
mode '0644'
action :create
end
end
行上的错误:
load_current_resource
和def load_current_resource
@current_resource = Chef::Resource::OmdCheck.new(@new_resource_name)
@current_resource.checkname(@new_resource.checkname) # right here!
@current_resource.sitename(@new_resource.sitename)
@current_resource.sitecfgroot(sprintf(CMK_CFGROOT_FRM, @new_resource.sitename))
@current_resource.perfometer(@new_resource.perfometer)
@current_resource.pnptemplate(@new_resource.pnptemplate)
@current_resource.exists = check_exists?(@current_resource.checkname)
end
方法只是为了表明 已初始化:
var routineMgr: routineManager = routineManager();
struct routine{
var name = "Name";
var tasks = [String]();
mutating func addTask(task: String){
tasks.append(task)
}
}
class routineManager: NSObject {
var routines = [routine]();
func addTask(name: String, desc: String){
routines.append(routine(name: name, tasks: ["Hallo","Moin"]));
routines[0].addTask("Salut")
}
}
非常感谢任何帮助。
答案 0 :(得分:1)
所以我得到了#chef的答案:
<@coderanger> Sammitch: Use current_resource, not @current_resource
< Sammitch> it's passed in as an instance var?
<@coderanger> Sammitch: No, its actually a method on Provider
<@coderanger> as is new_resource
<@coderanger> the issue is that the block on a resource is evaluated against the resource object
<@coderanger> So in there, @foo is looking at an ivar on the new resource object
<@coderanger> _but_ there is some magic
<@coderanger> Chef::Resource has a method_missing to forward unknown method calls to the enclosing provider
<@coderanger> So #current_resource gets forwarded up for you
<@coderanger> Basically never use the ivar form
<@coderanger> Always new_resource and current_resource instead
<@coderanger> and it will mostly JFW
就Ruby和Chef而言,我是绿色的,所以只有20%对我有意义,但我修改了我的代码到下面并且它有效:
def create_check
cookbook_file get_check_filename(@current_resource.checkname) do
source "checks/#{current_resource.checkname}" # removed the @
mode '0644'
action :create
end
end
答案 1 :(得分:0)
您通常必须将方法放入&#34;#{}&#34; in()
示例:
"#{method}" # does not work
"#{(method)}" # works
a = method; "#{a}" # works too