16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/ruby/array/columnize.rb', line 16
def columnize(columns=nil, opts={})
pre, ori, space = ColumnizeDefaults.merge(opts).values_at(:prepend, :orientation, :spacing)
columns ||= 2
numrows = length.quo(columns).ceil
sizes, print = Array.new(columns) { [] }, Array.new(numrows) { [] }
if ori == :vertical then
(self+Array.new(numrows*columns-length,"")).each_with_index { |e,i|
sizes[i.div(numrows)] << e
print[i%numrows] << e
}
else
(self+Array.new(numrows*columns-length,"")).each_with_index { |e,i|
sizes[i%columns] << e
print[i.div(columns)] << e
}
end
sizes = sizes.map { |e| e.map { |f| f.length }.max+space }
format = "#{pre}%-#{sizes.join('s %-')}s"
print.map { |e| format % e }.join("\n")
end
|