厨师食谱中使用了哪些红宝石功能? [英] what ruby features are used in chef recipes?

查看:57
本文介绍了厨师食谱中使用了哪些红宝石功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用厨师,对红宝石的了解不多。

I just started using chef and don't know much about ruby.

我在理解食谱中使用的语言语法时遇到问题。

I have problems understanding the language-syntax used in recipes.

说,我在食谱/default.rb中的食谱中创建目录,例如:

Say, I create a directory in a cookbook in recipes/default.rb like:

directory "/home/test/mydir" do
  owner "test"
  mode "0755"
  action :create
  recursive true
end

我认为这是有效的ruby脚本的一部分。 所有者测试 这样的行是什么意思?

I assume this is part of a valid ruby script. What do lines like owner "test" mean? Is this a function call, a variable assignment or something else entirely?

推荐答案

Chef是用Ruby编写的,并广泛使用了它? Ruby设计自定义DSL的能力。几乎每个厨师配置文件都是用基于Ruby的DSL编写的。

Chef is written in Ruby and makes an extensive use of Ruby ability to design custom DSL. Almost every chef configuration file is written with a Ruby-based DSL.

这意味着要有效地使用Chef,您应该熟悉Ruby语法的基本知识,包括

This means that in order to use chef effectively you should be familiar with the basic of Ruby syntax including


  • 语法

  • 数据类型(与其他语言的主要区别是符号)


您不需要了解很多有关Ruby中元编程的知识。

You don't need to know a lot about metaprogramming in Ruby.

您发布的代码案例是基于Ruby的DSL的一个很好的例子。让我解释一下。

The case of the code you posted is an excellent example of a Ruby based DSL. Let me explain it a little bit.

# Call the method directory passing the path and a block
# containing some code to be evaluated
directory "/home/test/mydir" do

  # chown the directory to the test user
  owner "test"

  # set the permissions to 0555
  mode "0755"

  # create the directory if it does not exists
  action :create

  # equivalent of -p flag in the mkdir
  recursive true

end

块是一种方便的方法,用于指定一组操作(在这种情况下,创建,设置权限等),这些操作将在单个上下文中(在这种情况下,在该路径的上下文中)进行评估。

Blocks are a convenient way to specify a group of operations (in this case create, set permissions, etc) to be evaluated in a single context (in this case in the context of that path).

这篇关于厨师食谱中使用了哪些红宝石功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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