Reputation: 761
Here's the ruby command I'd like to save the output of Time.now.strftime('%m%d%Y_%H%M')
I was thinking I could just add something like this to my recipe '
TODAY={ ::Time.now.strftime('%m%d%Y_%H%M') }
but this doesn't seem to work
==> 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)
eventually I want to make this an attribute so I can access it from multiple recipes
default['neustar-npac-deployment']['node_ohai_time'] = TODAY={ ::Time.now.strftime('%m%d%Y_%H%M') }
thanks!
Upvotes: 0
Views: 1181
Reputation: 29588
As per your request I am posting this as an answer.
Right now you are using {}
incorrectly as this is not a block but a Hash
literal which is why it is complaining. In order to make this a block you must use a lambda
or Proc
object.
lambda
lambda's can be created using one of 2 different syntax styles
-> { "This is a lambda" }
#=> #<Proc:0x2a954c0@(irb):1 (lambda)>
lambda { "This is also a lambda" }
#=> #<Proc:0x27337c8@(irb):2 (lambda)>
Either way is perfectly acceptable
Proc
Procs can be created using Proc.new { "This is a proc" }
for this question the semantic differences are not needed.
lambda
and Proc
will lazy evaluate the statement inside the block on #call
which means that the value can remain fluid.
Let's take your example:
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
I hope this in some way helps you better understand the concept.
Upvotes: 2