I’m trying to build an array of folders with the files contained in each. So if your directory structure is:
DirA
|- FileA
\- FileB
DirB
\- FileC
And I’m given this array:
files = ["DirA/FileA", "DirA/FileB", "DirB/FileC"]
I’m trying to build a hash like this
{DirA => [FileA, FileB], DirB => [FileC]}
Right now I’m doing it in what I think is a fairly non-Rubyish way (assume String has a method defined which fetches the parent directory):
h = {}
files.each do |f|
parent = f.getParentDir
if not h[parent] then h[parent] = [] end
h[parent].push f
end
Is there are more elegant way?
I would do