This commit is contained in:
Rikuoh Tsujitani 2024-04-15 22:48:09 +09:00
parent 86ffd14ae8
commit 0ff33d2496
Signed by: riq0h
GPG key ID: 010F09DEA298C717

91
ls.rb
View file

@ -1,56 +1,69 @@
# frozen_string_literal: true # frozen_string_literal: true
items = [] COLUMNS = 3
files = []
NUM_COLUMNS = 3 # 列幅の最大数 def run(files)
served = serve(files)
def main(items) sliced = slice(served)
taken_items = take_items(items) filled = fill(sliced)
sliced_items = slice_items(taken_items) arranged(filled)
sorted_items = sort_items(sliced_items)
display_items(sorted_items)
end end
def take_items(items) # def serve(files)
items.concat(Dir.glob('*')) # Dir.glob('*').each do |file|
# files = file
# end
# end
def serve(files)
files.concat(Dir.glob('*'))
end end
def slice_items(taken_items) def slice(served)
order_id = if (taken_items.size % NUM_COLUMNS).zero? element = if (served.size % COLUMNS).zero?
taken_items.size / NUM_COLUMNS.ceil # NUM_COLUMNSの倍数の時だけ、NUM_COLUMNSで割り込む served.size / COLUMNS
else else
taken_items.size / NUM_COLUMNS.ceil + 1 # 最大NUM_COLUMNS列に収める served.size / COLUMNS + 1
end end
taken_items.each_slice(order_id).to_a served.each_slice(element).to_a
end end
def sort_items(sliced_items) def fill(sliced)
if (0..NUM_COLUMNS).cover?(sliced_items.flatten.size) array_size = sliced.map(&:size).max
sliced_items.flatten sliced.each do |slice|
else slice << '' while slice.size < array_size
max_size = sliced_items.map(&:size).max
sliced_items.each do |item| # サブ配列の要素数を揃える
item << nil while item.size < max_size
end
end end
sliced_items.transpose sliced.transpose
end end
def display_items(sorted_items) def arranged(filled)
max_word_count = sorted_items.flatten.compact.map(&:size).max element2 = filled.flatten.map(&:size).max
if (0..NUM_COLUMNS).cover?(sorted_items.flatten.size) filled.each do |arrange|
sorted_items.flatten.map do |item| arrange.each do |arranged|
print item.to_s.ljust(max_word_count + 5) print arranged.ljust(element2 + 5)
end end
puts puts
else
sorted_items.each do |items|
items.compact.each do |item|
print item.ljust(max_word_count + 5)
end
puts
end
end end
end end
main(items) run(files)
# ls.rb:13:11: W: [Correctable] Lint/UnusedMethodArgument: Unused method argument - files. If it's necessary, use _ or _files as an argument name to indicate that it won't be used. If it's unnecessary, remove it. You can also write as serve(*) if you want the method to accept any arguments but don't care about them.
# def serve(files)
# ls.rb:27:27: C: [Correctable] Style/SymbolProc: Pass &:size as an argument to map instead of a block.
# array_size = sliced.map { |array| array.size }.max
# ^^^^^^^^^^^^^^^^^^^^^^
# ls.rb:34:1: C: Metrics/AbcSize: Assignment Branch Condition size for arranged is too high. [<5, 20, 6> 21.47/17]
# def arranged(filled) ...
# ^^^^^^^^^^^^^^^^^^^^
# ls.rb:34:1: C: Metrics/MethodLength: Method has too many lines. [14/10]
# def arranged(filled) ...
# ^^^^^^^^^^^^^^^^^^^^
# ls.rb:35:33: C: [Correctable] Style/SymbolProc: Pass &:size as an argument to map instead of a block.
# element2 = filled.flatten.map { |array| array.size }.max
# ^^^^^^^^^^^^^^^^^^^^^^
# ls.rb:43:32: W: Lint/ShadowingOuterLocalVariable: Shadowing outer local variable - arrange.
# arrange.compact.each do |arrange|
# ^^^^^^^