Skip to content
Open
Show file tree
Hide file tree
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
33 changes: 20 additions & 13 deletions 05.ls/ls.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env ruby

Choose a reason for hiding this comment

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

質問)この行、なぜ消したんでしょう?

# frozen_string_literal: true

require 'optparse'
Expand All @@ -9,22 +8,20 @@
FILE_STAT_CONVERSION = { '10' => '-', '04' => 'd', '12' => 'l' }.freeze

def main
l_option = false

params = ARGV.getopts('arl')
opts = OptionParser.new
opts.on('-l') { l_option = true }
opts.parse!(ARGV) || '.'
ls_dir = ARGV[0] || '.'

file_stats = make_file_stats(ls_dir)
if l_option
file_stats = make_file_stats(ls_dir, params)
if params['l']
print_l_option_files(file_stats)
else
print_no_option_files(file_stats)
print_not_l_option_files(file_stats)

Choose a reason for hiding this comment

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

man ls-lの説明を読んでみてください。
たぶんlはlong formatのlだと説明されているはずです。
なので、メソッド名もprint_in_long_format、print_in_short_formatみたいにすると対称性があって良いかもしれません。(好みの問題です)

end
end

def print_no_option_files(file_stats)
def print_not_l_option_files(file_stats)
file_names = file_stats.map { |file_stat| file_stat[:file_name] }
max_word_of_characters = file_names.max_by(&:length).length
max_row = file_names.size / MAX_COLUMN + 1
Expand All @@ -49,7 +46,7 @@ def print_l_option_files(file_stats)
print file_stat[:user].ljust(max_lengths[:user] + 2)
print file_stat[:group].ljust(max_lengths[:group] + 2)
print file_stat[:file_size].to_s.rjust(max_lengths[:file_size])
print file_stat[:time].strftime(' %b %e %R ')
print file_stat[:time].strftime(' %_m %e %R ')
puts file_stat[:file_name]
end
end
Expand All @@ -74,8 +71,8 @@ def convert_file_stat_chars(file_stat)
file_type + owner_permission + group_permission + other_permission
end

def make_file_stats(ls_dir)
files = fetch_files(ls_dir)
def make_file_stats(ls_dir, params)

Choose a reason for hiding this comment

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

「オブジェクトを作る」の「作る」はmakeよりもbuildが使われることが多いので、ここもbuild_xxxにした方がいいかも

files = fetch_files(ls_dir, params)
files.map do |file|
file_stat = File.stat(file)
file_stats = {}
Expand All @@ -91,8 +88,18 @@ def make_file_stats(ls_dir)
end
end

def fetch_files(ls_dir)
Dir.glob("#{ls_dir}/*").sort
def fetch_files(ls_dir, params)
files = if params['a']
Dir.glob("#{ls_dir}/*", File::FNM_DOTMATCH).sort
else
Dir.glob("#{ls_dir}/*").sort

Choose a reason for hiding this comment

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

flagのデフォルト値は数値の0のようです。
https://docs.ruby-lang.org/ja/latest/method/Dir/s/=5b=5d.html

なので、

flags = params['a'] ? File::FNM_DOTMATCH : 0

のようにすると、ここのコードがよりDRYにできそうです

あと、前回紹介したFile.joinを使ってパスを連結してみてください〜。

end

if params['r']
files.reverse
else
files

Choose a reason for hiding this comment

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

三項演算子を使ってもいいかもしれませんね

end
end

main
105 changes: 0 additions & 105 deletions 05.ls/test.rb

This file was deleted.