I have a set of files in a folder. I would like to pass an array of the files in a folder to some function. I saw the following example
$files= ["C:/dir/file1", "C:/dir/file2", "C:/dir/file3",
"C:/dir/file4", "C:/dir/file5"]
# function call with lambda:
$binaries.each |String $binary| {
file {"/usr/bin/$binary":
ensure => file,
}
}
but instead of declaring files manually, can I read all the files from a directory and pass it to some function?
You can use Dir
to fetch all files using some pattern. For example:
[1] pry(main)> Dir["/Users/smefju/tmp/*"]
=> ["/Users/smefju/tmp/a.rb",
"/Users/smefju/tmp/asd",
"/Users/smefju/tmp/bm.rb",
"/Users/smefju/tmp/cert",
"/Users/smefju/tmp/gc",
"/Users/smefju/tmp/qq"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.