-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepestin
More file actions
32 lines (20 loc) · 694 Bytes
/
deepestin
File metadata and controls
32 lines (20 loc) · 694 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env ruby
class Dir
def self.deepest(d)
deepest = Proc.new do |paths, depth|
# get all files in all dirs.
# if no file is found, these dirs are deepest
files = paths.map{ |path| Dir.glob(File.join(path, '*')) }.flatten
return [paths, depth+1] if files.count == 0
# these files are deepest when none of them is dir
dirs = files.select { |f| File.directory? f }
return [files, depth+1] if dirs.count == 0
# if any file is dir, it coild be deeper
deepest.call(dirs, depth+1)
end
# start process
deepest.call [d], 0
end
end
path = ARGV.shift
puts Dir.deepest(path)