在一个目录中创建多个文件夹

Create Multiple Folders Within One Directory

我正在尝试用尽可能少的 LOC 构建一个简单的文件结构, 我知道 FileUtils.mkdir_p './this/that/the/other' 开辟了道路。 Ruby中是否有任何东西可以在一个目录中创建多个文件夹,例如FileUtils.mkdir_p './this/{that, foo, bar}/the/other(这不起作用)?

你可以这样做

%w[this that next].each{|dir| Dir.mkdir "/path/to/somewhere/#{dir}"}

这将创建 /path/to/somewhere/this/path/to/somewhere/that/path/to/somewhere/next 个文件夹。

顺便一提,您一次只能创建一个文件夹。即您必须创建一个文件夹 ./foo,然后是 ./foo/bar,然后是 ./foo/bar/next

与ruby无关,是OS工作的常用方式

%w(that foo bar).each{|dir| FileUtils.mkdir_p( "./this/#{dir}/the/other")}

注意双引号,它们允许字符串插值(在字符串中执行代码)。