Ruby代码块和Chef [英] Ruby code blocks and Chef

查看:52
本文介绍了Ruby代码块和Chef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ruby和Chef的一个新手。我一直在努力地围绕语法进行研究,但我相信大家都知道,除非有人知道该术语,否则很难找到您想要的东西。

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.

我已经阅读了Ruby代码块,但是Chef代码块仍然让我感到困惑。我看到这样的例子:

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?

推荐答案

如果有人可以帮助您解释语法上的区别,并给我一些可以用来进行自我教育的术语?来自另一种语言(不是Ruby),这种语法可能看起来很奇怪。让我们分解一下。

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)等效于 foo bar

  • foo(bar,baz)等效于 foo bar,baz

  • foo(bar) is equivalent to foo bar
  • foo(bar, baz) is equivalent to foo bar, baz

Ruby代码块可以用大括号( {} )或 do..end 块,可以将其作为方法的最后一个参数传递给方法(但请注意,没有逗号,如果使用括号,它会在之后例如:

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

在某些情况下,代码块可以接收参数,但是在Chef中仅供参考,其语法为:

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

关于Chef资源,其语法通常是:

Specifically about Chef resources, their syntax is usually:

resource_type(name) do
  attribute1 value1
  attribute2 value2
end

这意味着,当您说:

log "a debug string" do
  level :debug
end

您实际上是在创建 log 资源,其 name 属性设置为调试字符串 。以后可以使用 log [调试字符串] 引用它(例如,在其他资源中)。

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, name 属性对于每个Chef资源类型都是必需的,因为这是使其唯一的原因,它使您可以在声明它之后调用其他操作。

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.

侧面说明:红宝石块对于Chef资源通常是可选的。如果您执行以下操作:

Side note: The ruby block is usually optional for a Chef resource. If you do something like:

directory "/some/path"

Chef将使用其默认属性(在 action:create 中)编译该资源,并尝试使用这些目录创建命名目录。

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

这篇关于Ruby代码块和Chef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆