Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用ruby块初始化chef属性?_Ruby_Variables_Attributes_Chef Infra - Fatal编程技术网

用ruby块初始化chef属性?

用ruby块初始化chef属性?,ruby,variables,attributes,chef-infra,Ruby,Variables,Attributes,Chef Infra,这是我想保存Time.now.strftime(“%m%d%Y\u%H%m”)的输出的ruby命令。我想我可以在我的配方中添加这样的东西 TODAY={ ::Time.now.strftime('%m%d%Y_%H%M') } 但这似乎不起作用 ==> default: [2015-04-10T17:53:44+00:00] ERROR: /tmp/vagrant-chef/eb36617d9c55f20fcee6cd316a379482/cookbooks/test-cookbook/

这是我想保存
Time.now.strftime(“%m%d%Y\u%H%m”)的输出的ruby命令。
我想我可以在我的配方中添加这样的东西

TODAY={ ::Time.now.strftime('%m%d%Y_%H%M') }
但这似乎不起作用

==> default: [2015-04-10T17:53:44+00:00] ERROR: /tmp/vagrant-chef/eb36617d9c55f20fcee6cd316a379482/cookbooks/test-cookbook/recipes/install_app.rb:12: syntax error, unexpected '}', expecting tASSOC
    ==> default: TODAY={ (Time.now.strftime('%m%d%Y_%H%M')) }
    ==> default:                                             ^
    ==> default: [2015-04-10T17:53:44+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
最后,我想把它作为一个属性,这样我就可以从多个食谱中访问它

default['neustar-npac-deployment']['node_ohai_time'] = TODAY={ ::Time.now.strftime('%m%d%Y_%H%M') }

谢谢

根据您的要求,我将此作为答复发布

现在您正在错误地使用
{}
,因为这不是一个块,而是一个
散列
文本,这就是它抱怨的原因。为了使其成为块,必须使用
lambda
Proc
对象

lambda

可以使用两种不同的语法样式之一创建lambda

-> { "This is a lambda" } 
#=> #<Proc:0x2a954c0@(irb):1 (lambda)>
lambda { "This is also a lambda" }
#=> #<Proc:0x27337c8@(irb):2 (lambda)>

我希望这能在某种程度上帮助你更好地理解这个概念

为什么有
{}
这不是块,而是散列文字语法。如果希望这是一个惰性块,请使用
->{}
lambda{}
,这两者都将生成
lambda
Proc.new{}
,这将生成一个真正的
Proc
。所以类似于
TODAY=->{Time.now.strftime(“%m%d%Y\u%H%m”)}
。然后要得到结果,它将是
今天。调用
,或者您可以直接将它设置为
今天=时间。now.strftime(“%m%d%Y_%H%m”)
,但您似乎希望它是延迟计算的,以便lambda可能最适合。我不介意它是否是延迟计算的,我只是在遵循一个示例,而不了解
{}
是用于。我将尝试直接使用它,看看它是否奏效。ThanksMy point was
TODAY=Time.now.strftime(“%m%d%Y\u%H%m”)
意味着
TODAY
在计算后不会更改,因为它是一个固定常量,其中as
TODAY=->{Time.now.strftime(“%m%d%Y\u%H%m”)}
表示您今天每次使用
时。调用
它将计算lambda并返回当前响应。这有意义吗?哦,我现在明白了,这很好!在这种情况下,我只需要它保持不变,但我会记住它以供将来参考。您应该将您的评论作为答案发布:)感谢您对proc和lambda的精彩介绍!
 NOW = Time.now.strftime('%m%d%Y_%H%M')
 # in this case NOW will be evaluated once and will always equal the 
 # string result of when it was first interpretted
 TODAY = -> {Time.now.strftime('%m%d%Y_%H%M')}
 # in this case TODAY is simply a lambda and it's value will be dependent
 # upon the time when you "call" it so something like this will clearly illustrate
 # the difference
 while NOW == TODAY.call
    puts "Still the same time" 
 end 
 # after this NOW will still reflect it's initial set value and for 
 # a while ~ 1 minute this will output "Still the same time" 
 # at some point TODAY.call will increment up by 1 minute because it is 
 # re-evaluated on each `#call` thus allowing it to change periodically with the clock