【问题标题】:Ruby blocks as used in Chef, trouble understanding syntax [duplicate]Chef中使用的Ruby块,难以理解语法[重复]
【发布时间】:2014-01-08 08:38:57
【问题描述】:

我对 Ruby 有点陌生,但我已经阅读了很多关于块的教程和文档。但是,我真的不明白下面的语法,这是厨师食谱中使用的:

template "/etc/profile.d/golang.sh" do
  source "golang.sh.erb"
  owner "root"
  group "root"
  mode 0755
end

我该如何解析呢? template 是什么?它是一个传递 2 个参数("/etc/profile.d/golang.sh" 和块)的函数吗? sourceowner等功能也一样吗?

抱歉,这是一个如此初级的问题!

【问题讨论】:

标签: ruby chef-infra


【解决方案1】:

我在https://stackoverflow.com/a/20569738/123527回答了同样的问题

# Call the method directory passing the path and a block
# containing some code to be evaluated in the given context
template "/etc/profile.d/golang.sh" do

  # Use the ERB template defined at "golang.sh.erb"
  source "golang.sh.erb"

  # chown the file to the user root
  owner "root"
  group "root"

  # set the permissions to 0555
  mode "0755"
end

块是指定一组操作(在本例中为创建、设置权限等)在单个上下文(在本例中为该路径的上下文)中评估的便捷方式。

templatesourceownergroup等都是Ruby方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    相关资源
    最近更新 更多