I am an extremely new person to Ruby and Chef. I have been trying to wrap my head around the syntax and do some research, but I am sure as you all know unless one knows the terminology, it is hard to find what you are looking for.
I have read up on Ruby code blocks, but the Chef code blocks still confuse me. I see something like this for example:
log "a debug string" do
level :debug
end
Which adds "a debug string" to the log. From what I have seen though, it seems to me like it should be represented as:
log do |message|
#some logic
end
Chef refers to these as resources. Can someone please help explain the syntax difference and give me some terminology from which I can start to educate myself with?
If you come from another language (not Ruby), this syntax might seem very strange. Let's break down things.
When calling a method with parameters, in most cases the parentheses are optional:
foo(bar)
is equivalent tofoo bar
foo(bar, baz)
is equivalent tofoo bar, baz
A Ruby block of code can be wrapped in curly braces (
{}
) or inside ado..end
block and can be passed to a method as its last parameters (but note that there's no comma and if you're using parentheses it goes after them. Some examples:In some cases, code blocks can receive parameters, but in Chef the resources' blocks never do. Just for reference, the syntax for that is:
Specifically about Chef resources, their syntax is usually:
This means that, when you say:
you're actually creating a
log
resource whosename
attribute is set to"a debug string"
. It can later be referred to (in other resources, for example) usinglog[a debug string]
.AFAIK, the
name
attribute is mandatory for every Chef resource type as it's what makes it unique, and allows you to, among other things, call actions on it after it has been declared.Side note: The ruby block is usually optional for a Chef resource. If you do something like:
Chef will compile that resource using its default attributes (among which is
action :create
), and try to create the named directory using those.The
do ... end
here is not a usual ruby block statement.It's a implementation of
DSL (Domain Specific Language)
.Here's a nice explanation [1]:
Hope that answer your question.
[1] : http://www.infoq.com/news/2007/06/dsl-or-not