Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions 04.ls/ls1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

def list_files
current_path = Dir.pwd
folder_glob = "#{current_path}/*"
files = Dir.glob(folder_glob)
files.map { |file| File.basename(file) }
end

def print_files_in_columns(columns = 3)
file_list = list_files

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
file_list = list_files
files = list_files
Suggested change
file_list = list_files
columns = Array.new(rows){[]}

「〜の配列」という名前を表現したいときは、複数形にすればOKです。(column_arraysも同様)

max_length = file_list.map { |file| file.length }.max || 0
column_width = (max_length + 2)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

変数の寿命が長いので、もっと短くしましょう。
変数の寿命はできるだけ短くしよう | FBC

rows = (file_list.length.to_f / columns).ceil
column_arrays = Array.new(rows){[]}

file_list.each_with_index do |file, index|
column_arrays[index % rows] << file
end

column_arrays.each do |row|
row.each do |col|
print col&.ljust(column_width)
end
puts
end
end

print_files_in_columns