Tintin81 Tintin81 - 4 months ago 40
Ruby Question

How to match multiple patterns with Dir.glob?

In my Rails app I am trying to collect the paths to all the files contained in two different directories using

Dir.glob
.

The following code works but is not very concise. Isn't there a way two match two patterns at once with
Dir.glob
?

common_file_paths = Dir.glob("app/assets/mystuff/*").reject do |path|
File.directory?(path)
end

more_file_paths = Dir.glob("app/assets/mystuff/more/*").reject do |path|
File.directory?(path)
end

file_paths = common_file_paths + more_file_paths

Answer

Dir.glob also accepts an array of patterns.

Dir.glob(["app/assets/mystuff/*", "app/assets/mystuff/more/*"])