简体   繁体   English

无法弄清楚ruby方法“目录”及其功能

[英]Unable to figure out ruby method “directory” and what it does

I am very new to ruby and was trying to understand some code when I got stuck at this snippet: 我对ruby很新,当我遇到这个片段时,我试图理解一些代码:

directory "test_dir" do
    action :create
    recursive true
end

I tried googling directory class but was unsuccessful. 我试过googling directory类,但没有成功。 I found a class Dir but its not the same. 我发现了一个类Dir但它不一样。 I see that intuitively this snippet should create a new directory and name it test_dir but I do not want to assume things and move forward. 我直观地看到这个片段应该创建一个新目录,并将其命名为test_dir但我不想假设并继续前进。

EDIT This was a part of a chef-recipe which is used to launch a particular task. 编辑这是用于启动特定任务的厨师配方的一部分。 For the purposes of launching, it needs to create a directory and download some jars to it. 出于启动的目的,它需要创建一个目录并下载一些罐子。 There is an execute method below which goes like this: 下面有一个execute方法,如下所示:

execute 'deploy' do
    action :nothing
    # ignore exit status of storm kill command
    command <<-EOH
      set -e
      storm kill #{name} -w 1 || true
      sleep 3
      storm jar #{points to the jar}
    EOH
end

Sorry I have to be a bit obfuscated as some of the things are not open sourced. 对不起,我必须有点混淆,因为有些东西不是开源的。

It's the Chef internal DSL for Directory management. 它是用于目录管理的Chef内部DSL。 Read more here: http://wiki.opscode.com/display/chef/Resources#Resources-Directory 在这里阅读更多内容: http//wiki.opscode.com/display/chef/Resources#Resources-Directory

PS: The recursive true tells it to create the folder much like mkdir -p . PS: recursive true告诉它创建文件夹很像mkdir -p

directory is not a class, it is a method. directory不是一个类,它是一个方法。 I do not know what module it is a part of, but that snippet is about equivalent to this: 我不知道它是哪个模块的一部分,但该片段大致相当于:

Kernel.directory.call("test_dir",lambda {action :create; recursive true})

That snippet uses some gem that adds a directory method to the Kernel object. 该片段使用一些gem将directory方法添加到Kernel对象。

As others have mentioned, it is part of the directory management DSL Chef . 正如其他人所提到的,它是目录管理DSL Chef的一部分 A DSL is a set of methods integrated into the Kernel object; DSL是集成到Kernel对象中的一组方法; because of the very flexible method calling syntax of Ruby, method calls can look a lot like language keywords. 因为调用Ruby的语法非常灵活,所以方法调用看起来很像语言关键字。 That makes task specific commands (Domain Specific Languages: DSL) look very nice in Ruby; 这使得特定于任务的命令(Domain Specific Languages:DSL)在Ruby中看起来非常好; easy to use and flexible. 易于使用和灵活。 Thus gems that add DSLs are very common. 因此,添加DSL的宝石非常普遍。

It is the Directory resource of the Chef framework. 它是Chef框架的Directory资源。 (DSL stands for domain-specific language. Ruby is well suited for them.) (DSL代表特定领域的语言.Ruby非常适合他们。)

The snippet you pasted is not really enough information to go on (need context; where is the snippet from?) 您粘贴的代码段不足以提供足够的信息(需要上下文;代码段在哪里?)

That said directory looks more like a method than a class. 那个directory看起来更像是一个方法而不是一个类。 First, it's lowercased and classes are CamelCased. 首先,它是小写的,类是CamelCased。

If it's a method, it's defined somewhere within the application. 如果它是一个方法,它在应用程序中的某个地方定义。 Have you tried something like this: 你尝试过这样的事情:

grep -r "def directory" ./ or grep -r "def directory" ./

grep -r "directory" ./| grep "def"

If not in the application itself, it would be defined in one of the application's dependencies ( grep -r "..." $GEM_HOME/gems instead) 如果不在应用程序本身,它将在应用程序的一个依赖项中定义( grep -r "..." $GEM_HOME/gems

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM