Ruby code blocks and Chef

2019-01-28 14:14发布

问题:

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?

回答1:

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 to foo bar
  • foo(bar, baz) is equivalent to foo bar, baz

A Ruby block of code can be wrapped in curly braces ({}) or inside a do..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:

foo(bar) { # code here }

foo(bar) do
  # code here
end

foo bar do
  # code here
end

foo do
  # code here
end

In some cases, code blocks can receive parameters, but in Chef the resources' blocks never do. Just for reference, the syntax for that is:

foo(bar) do |baz, qux|
  baz + qux
end

Specifically about Chef resources, their syntax is usually:

resource_type(name) do
  attribute1 value1
  attribute2 value2
end

This means that, when you say:

log "a debug string" do
  level :debug
end

you're actually creating a log resource whose name attribute is set to "a debug string". It can later be referred to (in other resources, for example) using log[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:

directory "/some/path"

Chef will compile that resource using its default attributes (among which is action :create), and try to create the named directory using those.



回答2:

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]:

there is the concept of an internal DSL, which uses the syntax of an exіsting language, a host language, such as Ruby. The means of the language are used to build constructs resembling a distinct language. The, already mentioned, Rake uses this to make code like this possible:

task :codeGen do        
  # do the code generation      
end

Hope that answer your question.

[1] : http://www.infoq.com/news/2007/06/dsl-or-not