用 ruby 块初始化 chef 属性?
initialize chef attribute with ruby block?
这是 ruby 命令我想保存 Time.now.strftime('%m%d%Y_%H%M')
的输出我想我可以在我的食谱中添加这样的东西'
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') }
谢谢!
根据您的要求,我将此作为答复发布。
现在您错误地使用了 {}
,因为这不是块而是 Hash
文字,这就是它抱怨的原因。为了使它成为一个块,您必须使用 lambda
或 Proc
对象。
λ
lambda 可以使用 2 种不同语法风格中的一种来创建
-> { "This is a lambda" }
#=> #<Proc:0x2a954c0@(irb):1 (lambda)>
lambda { "This is also a lambda" }
#=> #<Proc:0x27337c8@(irb):2 (lambda)>
两种方式都可以接受
过程
可以使用 Proc.new { "This is a proc" }
创建过程
对于这个问题,不需要语义差异。
lambda
和 Proc
将惰性评估 #call
块内的语句,这意味着该值可以保持可变。
让我们举个例子:
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
我希望这能以某种方式帮助您更好地理解这个概念。
这是 ruby 命令我想保存 Time.now.strftime('%m%d%Y_%H%M')
的输出我想我可以在我的食谱中添加这样的东西'
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') }
谢谢!
根据您的要求,我将此作为答复发布。
现在您错误地使用了 {}
,因为这不是块而是 Hash
文字,这就是它抱怨的原因。为了使它成为一个块,您必须使用 lambda
或 Proc
对象。
λ
lambda 可以使用 2 种不同语法风格中的一种来创建
-> { "This is a lambda" }
#=> #<Proc:0x2a954c0@(irb):1 (lambda)>
lambda { "This is also a lambda" }
#=> #<Proc:0x27337c8@(irb):2 (lambda)>
两种方式都可以接受
过程
可以使用 Proc.new { "This is a proc" }
对于这个问题,不需要语义差异。
lambda
和 Proc
将惰性评估 #call
块内的语句,这意味着该值可以保持可变。
让我们举个例子:
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
我希望这能以某种方式帮助您更好地理解这个概念。