-
Notifications
You must be signed in to change notification settings - Fork 320
/
ch-2.rb
executable file
·54 lines (50 loc) · 1 KB
/
ch-2.rb
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /usr/bin/ruby
require 'set'
def kdd(dirlist0)
dirlist = dirlist0.sort
fx = Hash.new
dirlist.each do |d|
Dir.new(d).each do |entry|
nn = entry
if File.directory?(d + "/" + entry) then
nn += '/'
end
if !fx.has_key?(nn) then
fx[nn]=Set.new
end
fx[nn].add(d)
end
end
mm = dirlist.length
out = [dirlist]
fx.keys.sort.each do |f|
if !(fx[f].length == mm) then
l = []
dirlist.each do |d|
if fx[f].include?(d) then
l.push(f)
else
l.push("")
end
end
out.push(l)
end
end
return out
end
def tabular(d)
columnlength = []
d.each do |row|
row.each_with_index do |col,i|
while columnlength.length <= i do
columnlength.push(0)
end
columnlength[i] = [columnlength[i],col.length].max
end
end
fs = columnlength.collect{|i| "%-" + i.to_s + "s"}.join(" | ")
d.each do |row|
puts(fs % row)
end
end
tabular(kdd(["dir_a","dir_b","dir_c"]))